Dynamic Web Control Event Handling

B

Brent Borovan

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;
}
 
V

Visar Gashi, MCP

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
 
B

Brent Borovan

Thanks for the help. I've tried your suggestion, and the events are still
not firing. Any idea on what could be wrong?

Brent
 
B

Brent Borovan

Just to clarify, it is the txtBxAmount_TextChanged(object sender,
System.EventArgs e) event that is not firing.

Brent
 
B

Brent Borovan

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top