UpdatePanel中的动态控件并不总是异步引发

本文关键字:异步 动态控件 UpdatePanel | 更新日期: 2023-09-27 18:18:57

我尝试创建一个动态表与一些文本框取决于对象列表。然后我将它添加到包含在UpdatePanel中的Panel中。

一切都很好,除了有些时候,回发是异步的,有些时候,所有的页面都重新加载。没有规则,有时在完全回发之前会工作两次,有时会更多。找不到符合此行为的逻辑。

这是我的一段aspx代码:

<asp:UpdatePanel ID="udpTableDechets" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Panel ID="pnlTableDechets" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>
下面是我的一段代码:
protected override void OnLoad(EventArgs e)
{
    generateTableDechets();
    base.OnLoad(e);
}
private void generateTableDechets()
{
    Table tbl = new Table { ID = "dechets", ClientIDMode = ClientIDMode.Static };
    TableRow trDec = new TableRow();
    tbl.Controls.Add(trDec);
    TableCell tdDecReel = new TableCell();
    trDec.Controls.Add(tdDecReel);
    TextBox txtDechet = new TextBox { ID = string.Concat("txtDechet_", product.Nom), ClientIDMode = ClientIDMode.Static, AutoPostBack = true };
    txtDechet.TextChanged+=new EventHandler(txtDechet_TextChanged);
    tdDecReel.Controls.Add(txtDechet);
    pnlTableDechets.Controls.Add(tbl);
}
protected void txtDechet_TextChanged(object sender, EventArgs e)
{
    // Get the value, and update the object containing values
    // Then update labels in table thanks to another method
}

更新1

实际上,我在静态中尝试了相同的方法,并且我有完全相同的行为。

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:TextBox runat="server" AutoPostBack="true" OnTextChanged="txt_TextChanged" />
    </ContentTemplate>
</asp:UpdatePanel>

你正常吗?这是一个已知的bug吗?我原谅了什么吗?我怎样才能确保每个textChanged请求将异步执行。提前感谢您的回答

更新2

当我按Enter键或当我突出显示文本框内容以替换它时,问题似乎发生了。

<<p> 解决方案/strong>

多亏了IPostBackEventHandler接口(见这里)。

我手动管理事件,并在RaisePostBackEvent()方法中捕获它。在这里,多亏了传入参数的控制ID,我可以做我的事情。

感谢您的回答

UpdatePanel中的动态控件并不总是异步引发

在创建包含动态控件的网站时,这是一个常见的问题。如果你想每次都触发事件,你应该从javascript调用后面的代码。因为每次都执行javascript。

function SaveClick() {
            Page.GetPostBackEventReference(objBtnSave);
            __doPostBack("objBtnSave", "OnClick");
        }

也许这会解决你的问题。

在静态中,我添加了ChildrenAsTriggers="true"属性,或者在每个动态控件上使用TextChanged事件指定AsyncPostBackTrigger。