updatepanel中的asp.net链接按钮不会触发 [英] asp.net linkbutton in updatepanel doesn't fire
问题描述
我有一个asp.net Web应用程序.在我的 .aspx
页面上,我有一个更新面板,其中有3个 asp:LinkButton
,应在后面调用c#代码.问题是onclick无法正常工作.
I have a asp.net web application. In my .aspx
page I have a update panel in which I have 3 asp:LinkButton
that should make a call to a c# code behind. The problem is that the onclick doesn't work.
这是代码的外观:
<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>
代码有什么问题?我也尝试过使用 AsyncPostBackTrigger
,但仍然无法正常工作.
What is wrong with the code? I have also tried using AsyncPostBackTrigger
but still doesn't work.
根本不调用后面的代码.
The code behind is not invoked at all.
我也曾尝试在Google上进行搜索,但找不到解决方案.
I have also tried to search on Google but couldn't find a solution.
推荐答案
您非常亲密.几件事:
- 您所说的触发器应该是AsyncPostBackTriggers.
- 您的触发器需要一个事件名称.
- 建议:这不会阻止事件触发,但是除非您希望每个可发布事件引起回发,否则请将UpdateMode ="Conditional"添加到UpdatePanel.
这是一个可行的示例.
Web表单-WebForm1.aspx:
Web Form - WebForm1.aspx:
<%@ 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>
代码隐藏-WebForm1.aspx.cs:
CodeBehind - WebForm1.aspx.cs:
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);
}
}
}
这篇关于updatepanel中的asp.net链接按钮不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!