|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Dynamic Web Control Event HandlingI am attemping to develop an ASP.net page that dynamically adds rows containing text box and/or listbox controls within each row. Each control within the table needs to be "wired" to the correct event handler. However, when we try the code as shown below, the event does not seem to fire (even tried to set a break point in the event handler code - the break point is never hit). The code appears to be correct, according to some code samples we have seen on the web. I'm relatively new to ASP.net and any help would greatly be appreciated. Thanks in advance. Code snippets follows... Brent ASPX file contains: <asp:Table ID="MyDynamicTable" Runat="server" backColor="#ffff99" Width="1080px" Height="25"></asp:Table> C# Code-Behind: protected System.Web.UI.WebControls.Table MyDynamicTable; protected System.Web.UI.WebControls.TextBox txtBxAmount; private void Page_Load(object sender, System.EventArgs e) { int numRows; int currRowNum; numRows = 5; for(currRowNum = 1; currRowNum <= numRows; currRowNum++) { TableRow tRow = new TableRow(); MyDynamicTable.Rows.Add(tRow); TableCell tcAmount = new TableCell(); tRow.Cells.Add(tcAmount); // cteate new instance of textbox txtBxAmount = new System.Web.UI.WebControls.TextBox(); txtBxAmount.ID = currRowNum.ToString(); txtBxAmount.Text = "Text box # = " + currRowNum.ToString(); txtBxAmount.TextChanged += new EventHandler(txtBxAmount_TextChanged); tcAmount.Controls.Add(txtBxAmount); } } private void txtBxAmount_TextChanged(object sender, System.EventArgs e) { // Let set if the event fired txtBxAmount.BackColor = System.Drawing.Color.Aqua; } Hello,
The Page Load event handler fires every time a postback occurrs on the page, including the event you are trying to handle. You should create controls OnInit instead. Asp.Net is "smart" about controls being generated that way. For example, see how controls that you drag and drop on a form are being initialized (the code is regionalized and hidden by default). Regards, -Visar Show quoteHide quote "Brent Borovan" wrote: > Good day, > > I am attemping to develop an ASP.net page that dynamically adds rows > containing text box and/or listbox controls within each row. Each control > within the table needs to be "wired" to the correct event handler. However, > when we try the code as shown below, the event does not seem to fire (even > tried to set a break point in the event handler code - the break point is > never hit). > > The code appears to be correct, according to some code samples we have seen > on the web. > > I'm relatively new to ASP.net and any help would greatly be appreciated. > > Thanks in advance. Code snippets follows... > Brent > > ASPX file contains: > <asp:Table ID="MyDynamicTable" Runat="server" backColor="#ffff99" > Width="1080px" Height="25"></asp:Table> > > C# Code-Behind: > protected System.Web.UI.WebControls.Table MyDynamicTable; > protected System.Web.UI.WebControls.TextBox txtBxAmount; > > private void Page_Load(object sender, System.EventArgs e) > { > int numRows; > int currRowNum; > numRows = 5; > > for(currRowNum = 1; currRowNum <= numRows; currRowNum++) > { > TableRow tRow = new TableRow(); > MyDynamicTable.Rows.Add(tRow); > > TableCell tcAmount = new TableCell(); > tRow.Cells.Add(tcAmount); > > // cteate new instance of textbox > txtBxAmount = new System.Web.UI.WebControls.TextBox(); > txtBxAmount.ID = currRowNum.ToString(); > txtBxAmount.Text = "Text box # = " + currRowNum.ToString(); > txtBxAmount.TextChanged += new EventHandler(txtBxAmount_TextChanged); > tcAmount.Controls.Add(txtBxAmount); > } > } > > private void txtBxAmount_TextChanged(object sender, System.EventArgs e) > { > // Let set if the event fired > txtBxAmount.BackColor = System.Drawing.Color.Aqua; > } Thanks for the help. I've tried your suggestion, and the events are still
not firing. Any idea on what could be wrong? Brent Show quoteHide quote "Visar Gashi, MCP" wrote: > Hello, > > The Page Load event handler fires every time a postback occurrs on the page, > including the event you are trying to handle. You should create controls > OnInit instead. Asp.Net is "smart" about controls being generated that way. > > For example, see how controls that you drag and drop on a form are being > initialized (the code is regionalized and hidden by default). > > Regards, > -Visar > > "Brent Borovan" wrote: > > > Good day, > > > > I am attemping to develop an ASP.net page that dynamically adds rows > > containing text box and/or listbox controls within each row. Each control > > within the table needs to be "wired" to the correct event handler. However, > > when we try the code as shown below, the event does not seem to fire (even > > tried to set a break point in the event handler code - the break point is > > never hit). > > > > The code appears to be correct, according to some code samples we have seen > > on the web. > > > > I'm relatively new to ASP.net and any help would greatly be appreciated. > > > > Thanks in advance. Code snippets follows... > > Brent > > > > ASPX file contains: > > <asp:Table ID="MyDynamicTable" Runat="server" backColor="#ffff99" > > Width="1080px" Height="25"></asp:Table> > > > > C# Code-Behind: > > protected System.Web.UI.WebControls.Table MyDynamicTable; > > protected System.Web.UI.WebControls.TextBox txtBxAmount; > > > > private void Page_Load(object sender, System.EventArgs e) > > { > > int numRows; > > int currRowNum; > > numRows = 5; > > > > for(currRowNum = 1; currRowNum <= numRows; currRowNum++) > > { > > TableRow tRow = new TableRow(); > > MyDynamicTable.Rows.Add(tRow); > > > > TableCell tcAmount = new TableCell(); > > tRow.Cells.Add(tcAmount); > > > > // cteate new instance of textbox > > txtBxAmount = new System.Web.UI.WebControls.TextBox(); > > txtBxAmount.ID = currRowNum.ToString(); > > txtBxAmount.Text = "Text box # = " + currRowNum.ToString(); > > txtBxAmount.TextChanged += new EventHandler(txtBxAmount_TextChanged); > > tcAmount.Controls.Add(txtBxAmount); > > } > > } > > > > private void txtBxAmount_TextChanged(object sender, System.EventArgs e) > > { > > // Let set if the event fired > > txtBxAmount.BackColor = System.Drawing.Color.Aqua; > > } Just to clarify, it is the txtBxAmount_TextChanged(object sender,
System.EventArgs e) event that is not firing. Brent Show quoteHide quote "Brent Borovan" wrote: > Thanks for the help. I've tried your suggestion, and the events are still > not firing. Any idea on what could be wrong? > > Brent > > "Visar Gashi, MCP" wrote: > > > Hello, > > > > The Page Load event handler fires every time a postback occurrs on the page, > > including the event you are trying to handle. You should create controls > > OnInit instead. Asp.Net is "smart" about controls being generated that way. > > > > For example, see how controls that you drag and drop on a form are being > > initialized (the code is regionalized and hidden by default). > > > > Regards, > > -Visar > > > > "Brent Borovan" wrote: > > > > > Good day, > > > > > > I am attemping to develop an ASP.net page that dynamically adds rows > > > containing text box and/or listbox controls within each row. Each control > > > within the table needs to be "wired" to the correct event handler. However, > > > when we try the code as shown below, the event does not seem to fire (even > > > tried to set a break point in the event handler code - the break point is > > > never hit). > > > > > > The code appears to be correct, according to some code samples we have seen > > > on the web. > > > > > > I'm relatively new to ASP.net and any help would greatly be appreciated. > > > > > > Thanks in advance. Code snippets follows... > > > Brent > > > > > > ASPX file contains: > > > <asp:Table ID="MyDynamicTable" Runat="server" backColor="#ffff99" > > > Width="1080px" Height="25"></asp:Table> > > > > > > C# Code-Behind: > > > protected System.Web.UI.WebControls.Table MyDynamicTable; > > > protected System.Web.UI.WebControls.TextBox txtBxAmount; > > > > > > private void Page_Load(object sender, System.EventArgs e) > > > { > > > int numRows; > > > int currRowNum; > > > numRows = 5; > > > > > > for(currRowNum = 1; currRowNum <= numRows; currRowNum++) > > > { > > > TableRow tRow = new TableRow(); > > > MyDynamicTable.Rows.Add(tRow); > > > > > > TableCell tcAmount = new TableCell(); > > > tRow.Cells.Add(tcAmount); > > > > > > // cteate new instance of textbox > > > txtBxAmount = new System.Web.UI.WebControls.TextBox(); > > > txtBxAmount.ID = currRowNum.ToString(); > > > txtBxAmount.Text = "Text box # = " + currRowNum.ToString(); > > > txtBxAmount.TextChanged += new EventHandler(txtBxAmount_TextChanged); > > > tcAmount.Controls.Add(txtBxAmount); > > > } > > > } > > > > > > private void txtBxAmount_TextChanged(object sender, System.EventArgs e) > > > { > > > // Let set if the event fired > > > txtBxAmount.BackColor = System.Drawing.Color.Aqua; > > > } Please disregard my previous posts. The control does not handle text change
event on key press, only on text change followed by loss of focus of the control.
Question about IE Web Controls Toolbar.
Displaying label at runtime MasterPages in ASP.NET 2.0 Need a list of radiobuttons with a specfic format asp.net 1.1 wizard control Alignment of a System.Web.UI.WebControls.Image object Datagrid problem Iterate TreeView element with 'disabled' property doesnt exist for form processing ? Dynamically added WebControls requiring TWO clicks to fire event?!? |
|||||||||||||||||||||||