我尝试根据对象列表创建带有一些文本框的动态表。然后我将它添加到包含在 UpdatePanel 中的面板中。
一切都很好,除了有时回发是异步的,有时所有页面都重新加载。没有规则,有时它会在完全回发之前工作两次,有时会更多。找不到这种行为的逻辑。
这是我的一段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>
对你来说正常吗?这是一个已知的错误吗?我原谅了什么吗?我怎样才能确保每个 textChanged 请求都将异步执行。预先感谢您的回答
更新 2
当我按 Enter 键或突出显示文本框内容以替换它时,似乎会出现问题。
解决方案
多亏了 IPostBackEventHandler 接口,我终于做到了(见这里)。
我手动管理事件,并在 RaisePostBackEvent() 方法中捕获它。所以在这里,多亏了传入参数的控件ID,我可以做我的事情了。
谢谢您的回答