Avatar of Member_2_991572

asked on 

Attaching event handlers to dynamically created controls in ASP.NET (C#)

I have been pulling my hair out for the past few days writing an ASP.NET C# page that dynamically adds controls (textboxes, ddlists, etc) to a form depending on values stored in a database.  The problem I am having is figuring out how to get the data from the controls once the user clicks the submit button.  I have settled on attaching an event to each dynamically generated textbox for when the text of each textbox changes.  Here is my code:

    protected override void OnInit(EventArgs e)

  protected void GenerateReportForm()
       //query db for number of textboxes and properties, store results in ReportsDataSet textBoxConrols -- code omitted for simplicity

        foreach (ReportsDataSet.TextBoxRow tbRow in textBoxControls)
            Label tempLabel = new Label();
            tempLabel.Text = tbRow.Label;
            tempLabel.ID = "tbl" + tbRow.ID.ToString();
            TextBox tempTextBox = new TextBox();
            tempTextBox.ID = tbRow.EMfield.ToString();
            tempTextBox.TextChanged += new EventHandler(TextBox_TextChanged);
            form1.Controls.Add(new LiteralControl("<br>"));

      protected void TextBox_TextChanged(object sender, EventArgs e)
          TextBox tempTextBox = (TextBox)sender;
          Session.Add(tempTextBox.ID.ToString(), tempTextBox.Text.ToString()); //store text into persistent session variable

I don't understand why the TextBox_TextChanged function is not being called after clicking the form's submit button.  Instead, the code for the submit button click handler is run.  Shouldn't the textbox text changed handler run first?  My Page_Load method contains no code.

I would really appreciate anyone's input on this, it has been killing me the past couple days.  Suggestions for a better way to grab dynamically created controls (user input) data would also be work, just need to get the text in those textboxes one way or another!  Thanks!
.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
Avatar of Odessa_Tech

There are 2 ways to populate the control. one is through server side event and the other through client side event. By just seeing your code it seems it is done in server side
it will work fine. But on TextChanged event,if you want to populate someother control with the value from this Textbox, then dont put it in a session instead display it in the targeted control i.e,Textboxintsance.Text

Other way around is using the client side event. on keypress event of the textbox, take the value from the current control and assign it to the required control
Code would like this

temptextbox.Attributes["OnKeyPress"] = "KeyPress_TextChanged('"+temptextbox.ClientID+"',someothercontrol.clientID)";

in the client side
function KeyPress_TextChanged(currenttextboxid,toreplacecontrol)

var currenttextboxidIns=  document.getElementId(currenttextboxid).value;
var toreplacecontrolIns=  document.getElementId(toreplacecontrol).value;
toreplacecontrolIns = currenttextboxidIns;
return false;


Is this you are looking for???
Is it because the control is re-created on postback?
What about this:
protected override void OnInit(EventArgs e)
  if (!IsPostBack) GenerateReportForm();

Open in new window

Avatar of Member_2_991572


Odessa_Tech -

Thank you for your input but I don't want to necessarily take the text from the textbox and put it in another control, I just want to grab the text itself.  Seems on postback I am losing that text.  Also, the bigger problem is that the event handler Check_Changed isn't running at all.

PhilipJonathan -

I have tried your suggestion before and it still doesn't work.  I can't find the controls after postback even if I create in OnInit only when IsPostBack is false.  

Thanks for the help!
Is the property tempTextBox.AutoPostBack set to true?

Btw, just a side remark. I suppose you are trying to achieve postback whenever a character is typed to the textbox. This would cause a lot of postback.
Avatar of Odessa_Tech

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo