Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving value from dynamic textbox

Status
Not open for further replies.

blueark

Technical User
Apr 16, 2002
1,212
IE
This one's driving me nuts!

I've got a user control that dynamically creates a number of textboxes. How do I retrieve the values from these text boxes when the form is submitted?

From looking through this forum, it appears that dynamic controls have to be recreated each time the page loads, but doing this clears the values of the text boxes. I've also tried setting up an onclick handler for the submit button in the vain hope that it might be executed before the dynamic controls disappeared, but still no success.

Any ideas?!
 
where are the textboxes getting the values from?? are they being calculated???

can you save them to a Session variable?? cookies maybe???
 
blu: a few links over at the ASP.NET forum; might find an insight in one of these articles:

..reading dynamically created textboxes...

..deals w/checkboxes but similar approach...

..associated Viewstate problems

...referencing properties
 
Thanks for the replies. Initially, the textboxes are getting their values from a database. The user can then change the values, and when the form is submitted, the database is updated with these new values. So, although I can recreate the textboxes with the original data, I'm having trouble retrieving the newly entered data.

Some of those links are interesting and have given me a few ideas about where to go from here. I'll try them out tomorrow and let you know how I get on.

Thanks again.
 
As a last resort, you can always get them values from the Request.Forms collection, although that's not very elegant.
Code:
foreach(string parm in Request.Form.AllKeys){
   if(!param.IndexOf("WhatIDYouGaveTheTextBox")== -1)
   {
       string textBoxValue = Request.Form[param].ToString();
   }
}

HTH
 
Assumming that you submit the form by clicking on a button:
Code:
html:
<form runat=&quot;server&quot; name=&quot;register&quot; ID=&quot;Form1&quot;>
  <asp:PlaceHolder ID=&quot;myPlaceholder&quot; Runat=server />
  <br>
  <asp:Button ID=btn Runat=server Text=&quot;Submit Form&quot; />
</form>

code behind:
private void Page_Load(object sender, System.EventArgs e)
{	
  createTextBox();					
}

private void createTextBox()
{
  TextBox tb = new TextBox();
  tb.ID = &quot;myTextBox&quot;;			
  myPlaceholder.Controls.Add(tb);
}

private void btn_Click(object sender, System.EventArgs e)
{
  // retrieve text from dynamic textbox after submitting the form
  TextBox txt = (TextBox)myPlaceholder.FindControl(&quot;myTextBox&quot;);
  string theText = txt.Text;
  Response.Write(theText);
}

private void InitializeComponent()
{    
  this.btn.Click += new System.EventHandler(this.btn_Click);
  this.Load += new System.EventHandler(this.Page_Load);
}
 
Thanks everyone, I think I'm starting to get somewhere now. I suspect my code is just getting a little complicated and the problem may be related to having user controls within user controls. I've created a much simpler example that works fine, so I'm going to use that as a starting point and build on it.
 
blueark,

I know exactly what you mean, for I am in exactly the same situation as you. I have usercontrols within usercontrols too. Have you figured this one out yet? Also, how are you storing these dynamic textbox values in a ViewState so they get re-read into the same dynamic textbox on postback? Thanks.

-- Brian

regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top