简体   繁体   English

ASP / C#-未触发UpdatePanel和按钮OnClick事件

[英]ASP/C# - UpdatePanel and Button OnClick Event Not Triggered

I've been trying to find the solution but it would be great if someone can take a look. 我一直在寻找解决方案,但是如果有人可以看一下,那就太好了。

In my aspx page and C# codebehind I have the following: 在我的aspx页面和C#代码背后,我有以下内容:

aspx: aspx:

<asp:UpdatePanel runat="server" ID="UpdatePanel8" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnAddTableRow" EventName="Click" />
    </Triggers>
    <ContentTemplate>
        <div id="divDynamicFields" runat="server"></div>
    </ContentTemplate>
</asp:UpdatePanel>

<div hidden>
    <asp:Button ID="btnAddTableRow" runat="server" OnClick="AddTableRow" />
</div>

<script type="text/javascript">
    function addTableRow(tableId) {
        $('#<%=btnAddTableRow.ClientID%>').click();
    }
</script>

C#: C#:

protected void AddTableRow(object sender, EventArgs e)
{
    (...)
}

The event is triggered if I don't use UpdatePanel, but when executed with UpdatePanel, there is PostBack but the C# method is not called. 如果我不使用UpdatePanel,则会触发该事件,但是使用UpdatePanel执行时,会出现PostBack,但不会调用C#方法。 I've tried to understand it for some time with no avail. 我试图了解它一段时间没有用。 Any ideas? 有任何想法吗? Thank you. 谢谢。

After a good night sleep and a fresh mind, I finally found out what is failing. 经过一夜的睡眠和崭新的思想,我终于发现了失败的原因。 The JS function addTableRow(tableId) is actually called by buttons that are dynamically created in Page_Load (the number of these buttons are not fixed, hence associating them with this JS function which clicks the hidden button that triggers the event method from codebehind). JS函数addTableRow(tableId)实际上是由在Page_Load中动态创建的按钮调用的(这些按钮的数量不是固定的,因此将它们与此JS函数关联,该JS函数单击隐藏的按钮从代码背后触发事件方法)。 Problem is, I was generating these buttons in the following way: 问题是,我以以下方式生成这些按钮:

Button addRowButton = new Button();
addRowButton.Text = "This is my dynamically generated button";
addRowButton.Attributes.Add("onclick", "addTableRow('" + idControl + "')");

But things started working when I've changed to: 但是当我更改为:

HtmlGenericControl addRowButton = new HtmlGenericControl("input");
addRowButton.Attributes.Add("type", "button");
addRowButton.Attributes.Add("onclick", "addTableRow('" + idControl + "');");
addRowButton.Attributes.Add("value", "This is my dynamically generated button");

It's actually kind of strange, since the button created with Button() still invoked JS function addTableRows and caused PostBack but didn't invoke the code behind method. 这实际上有点奇怪,因为用Button()创建的按钮仍然调用JS函数addTableRows并导致PostBack,但没有调用方法后面的代码。 Perhaps it had to do with the page life cycle and the generated IDs for the dynamic buttons that are generated in different ways depending on creating them as Button or HtmlGenericControl, but in any ways it's working now. 也许这与页面生命周期和动态按钮的生成ID有关,动态按钮的生成方式不同,具体取决于将它们创建为Button还是HtmlGenericControl,但现在以任何方式都可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM