Home All Groups Group Topic Archive Search About

Dynamic Web Control Event Handling

Author
8 Jun 2005 6:07 PM
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;
}

Author
8 Jun 2005 6:52 PM
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

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;
> }
Author
10 Jun 2005 1:15 PM
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

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;
> > }
Author
10 Jun 2005 1:33 PM
Brent Borovan
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;
> > > }
Author
10 Jun 2005 3:59 PM
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.