我有一个asp.net Web应用程序.在我的.aspx页面中,我有一个更新面板,其中我有3个asp:LinkButton,它应该调用c#代码.问题是onclick不起作用.
<div id="div1"> <asp:UpdatePanel ID="UpdatePanel2" runat="server"> <ContentTemplate> <ul> <li><asp:LinkButton ID="lnk_1" runat="server" OnClick="lnk1_Click">Link1</asp:LinkButton></li> <li><asp:LinkButton ID="lnk_2" runat="server" OnClick="lnk2_Click">Link2</asp:LinkButton></li> <li><asp:LinkButton ID="lnk_3" runat="server" OnClick="lnk3_Click">Link3</asp:LinkButton></li> </ul> <div> some more code here </div> </ContentTemplate> <Triggers> <asp:PostBackTrigger ControlID="lnk_1" /> <asp:PostBackTrigger ControlID="lnk_2" /> <asp:PostBackTrigger ControlID="lnk_3" /> </Triggers> </asp:UpdatePanel> </div>
解决方法
你很近.几件事:
>您的触发器应该是您尝试过的AsyncPostBackTriggers.
>您的触发器需要一个事件名称.
>建议:这不会阻止您的事件被触发,但除非您希望每个可以发生的事件导致回发,否则请将UpdateMode =“Conditional”添加到您的UpdatePanel.@H_404_3@
这是一个有效的例子.@H_404_3@
Web表单 – WebForm1.aspx:@H_404_3@
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AspDotNetStorefront.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager runat="server"></asp:ScriptManager> <div id="div1"> <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional"> <ContentTemplate> <ul> <li><asp:LinkButton ID="lnk_1" runat="server" OnClick="lnk1_Click">Never clicked</asp:LinkButton></li> </ul> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="lnk_1" EventName="Click" /> </Triggers> </asp:UpdatePanel> </div> </form> </body> </html>
CodeBehind – WebForm1.aspx.cs:@H_404_3@
using System; namespace AspDotNetStorefront { public partial class WebForm1 : System.Web.UI.Page { private static int _clickedCount = 0; protected void lnk1_Click(object sender,EventArgs e) { ++_clickedCount; var suffix = _clickedCount <= 1 ? "time" : "times"; lnk_1.Text = string.Format("Clicked {0} {1}",_clickedCount,suffix); } } }