0

This code a have written for an asp.net website, v2005

System.Web.UI.WebControls.TextBox txtEFName = new System.Web.UI.WebControls.TextBox();
phFname.Controls.Add(txtEFName);
placeHolder1.Controls.Add(TextBox1);

This code when executed, always shows the value of the textbox "" even if I enter some string.

Please help.

3
  • When / where are you trying to read back the value? And when / where is the code that adds the TextBox executed? Commented Oct 3, 2013 at 19:10
  • Where do you create this textbox? And where are you retrieving the value of the dynamically created textbox? Commented Oct 3, 2013 at 19:10
  • I'm flipping the value of the placeholder from a label to textbox on a click of button and then reading the value of the textbox by the click of the same button Commented Oct 3, 2013 at 20:26

1 Answer 1

3

Dynamic controls need to be re-created each time the page loads. So you need to have that code execute during the Page_Init event. Note: You need to assign a unique ID for this control that stays the same each time the page loads.

Why all this?

Because the control is not contained in the markup (the .aspx file), you need to add it again every time the page loads. So how does it retain its value? The value will be stored in the ViewState, and as long as the control has the same ID it will be repopulated with the correct value.

To keep things running smoothly, let's put the code for adding the control in a separate function.

Private void AddMyControl()
{
   System.Web.UI.WebControls.TextBox txtEFName = new System.Web.UI.WebControls.TextBox();
   txtEFName.ID = something unique;
   phFname.Controls.Add(txtEFName);
}

So we can call it from both the click handler and the Page_Init handler, but we only need to call it in the Page_Init if we have already clicked. So let's store that as a flag inside a Session variable (you can also store it in the ViewState if you like, but let's keep it that way for now). So our click handler will now look something like this:

void ButtonSomething_Click(Object Sender, EventArgs e)
{
   AddMyControl();
   Session["MyControlFlag"] == true;
}

Now we need our Page_Init handler:

Public void Page_Init(Object Sender, EventArgs e)
{
   if(Session["MyControlFlag"]!=null && (bool)Session["MyControlFlag"])
      AddMyControl();
}
Sign up to request clarification or add additional context in comments.

10 Comments

It should be assigned an ID as well.
I need to execute this code on the click of a button. After assigning an id also its not working.
@Sarfaraz - You'll need to populate it again in the Page_Init with the same Id. One way of doing it is to make a boolean flag that you store in a session or a hidden field. When you click the button you flip that flag to true, and in the Page_Init event you check for that flag and if true add the control again. Makes sense?
But populating it again will set it again to "". Isn't it?
@Sarfaraz - You're not populating it again. You are adding it back to the page (because it's not contained in the aspx markup). The ViewState will then populate it with the previous value as long as it has the same ID.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.