我有动态创建的TextBox的这个问题.
当在PageLoad中创建TextBox时,它被TextChanged事件触发.
但是当我动态地删除并重新创建了TextBox时,TextChanged没有触发.
这是代码:
的.aspx
<body> <form id="form1" runat="server"> <div> <asp:Table ID="Table1" runat="server"> <asp:TableRow> <asp:TableCell ColumnSpan="2">Fixed content</asp:TableCell> </asp:TableRow> </asp:Table> </form> </body>
的.cs
public partial class test:System.Web.UI.Page
{
string myText =“a”;
protected void Page_Load(object sender,EventArgs e) { WriteRows(); } private void WriteRows() { TableRow tr = new TableRow(); TableCell tc = new TableCell(); TextBox txt = new TextBox(); txt.Text = myText; txt.TextChanged += new EventHandler(txt_TextChanged); // Assign event handler tc.Controls.Add(txt); tr.Controls.Add(tc); tc = new TableCell(); tc.Text = txt.Text; tr.Controls.Add(tc); Table1.Controls.AddAt(1,tr); } private void txt_TextChanged(object sender,EventArgs e) { myText = ((TextBox)sender).Text; RedrawTable(); // Delete the row (incl. the TextBox) and rewrite it } private void RedrawTable() { Table1.Controls.RemoveAt(1); WriteRows(); } }
有没有人有解决方案,以便事件总是被触发?