469,903 Members | 2,023 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Dynamic Controls in ASP.NET

How can I capture user entered values from dynamically created controls in
ASP.NET ?

Thanks,
Nov 18 '05 #1
4 1753
you can get at them through Request.Form[]
when i did this, i found it useful to prefix all the controls i wanted to
access with the same thing, like txt1, txt2, txt3 etc.
that way in your code you can do something like:
foreach(string s in Request.Form.AllKeys)
{
if(s.IndexOf("txt") >= 0)
{
string formValue = Request.Form[s].ToString();
......
}
}

hth

tim

\\ email: tim at mackey dot ie //
\\ blog: http://tim.mackey.ie //
"Bhavin" <Bh****@discussions.microsoft.com> wrote in message
news:99**********************************@microsof t.com...
How can I capture user entered values from dynamically created controls in
ASP.NET ?

Thanks,

Nov 18 '05 #2
Depending on the control, you can capture the input in a number of ways. For
example, if you have a datarepeater with a textbox control and a button, you
can set your all of your buttons to have the same command event handler,
process your input that way. Datagrids work similarly. If you html controls
where runat is not runat="server", you can may need to iterate thru controls
to find the control's uniqueID, then call Request.Forms["ThatUniqueID"].

Help us understand your scenario a little better by proving more info on how
your controls are set up.

Alex

"Bhavin" <Bh****@discussions.microsoft.com> wrote in message
news:99**********************************@microsof t.com...
How can I capture user entered values from dynamically created controls in
ASP.NET ?

Thanks,

Nov 18 '05 #3
Based on your feedback, I think I need to give more info. Here it is

totalRows – integer value for number of records return by SQL select
statement.

Textbox[] txbox = new Textbox [totalRows];

for(int i=0; i < totalRows; i++){
txbox[i] = new Textbox();
txbox[i].ID = "myTextbox"+i;
}

I used above code in my page_load event to dynamically create array of
textboxes in place holder on my .aspx page using C# language(code behing
page). But once all dynamic empty textboxes got created I entered values in
them. Now I want to capture those values using dynamic IDs of textboxes but
those IDs no longer available after page_load event. I would like to capture
all values from textboxes to INSERT/UPDATE data in SQL table.

I have never used Request.Form before. I hope It gives you enough info so
you can understand my issue properly.

Thanks
"Alex" wrote:
Depending on the control, you can capture the input in a number of ways. For
example, if you have a datarepeater with a textbox control and a button, you
can set your all of your buttons to have the same command event handler,
process your input that way. Datagrids work similarly. If you html controls
where runat is not runat="server", you can may need to iterate thru controls
to find the control's uniqueID, then call Request.Forms["ThatUniqueID"].

Help us understand your scenario a little better by proving more info on how
your controls are set up.

Alex

"Bhavin" <Bh****@discussions.microsoft.com> wrote in message
news:99**********************************@microsof t.com...
How can I capture user entered values from dynamically created controls in
ASP.NET ?

Thanks,


Nov 18 '05 #4
Here's what I would try. As you look at your page's view source, you'll
notice your textboxes look something like this... <input
name="_ctl1:tbxPrice" type="text" value="152000" id="_ctl1_tbxPrice"
style="width:200px;" />. The ID of your textbox is dynamic, but you'll
notice the latter half of it is the ID you gave it. On your page post back,
you can get the inputs of the textboxes by iterating through them. To do
this, you know the names of your textboxes, because you created their names,
so basically have a general idea of what they are. What I would do is
determine which control (TextBox) is the one I need. To do this, I would
setup a regular expression, and iterate through the controls. Assuming your
dynamic textbox is called "_ctl1:tbxPrice" but it was "tbxPrice" when you
created it, I would do this.

Regex reg = new Regex( "(tbxPrice$)" ); //this finds the instance of
tbxPrice in a name, as in _ctl1:tbxPrice
foreach( Control c in this.Controls ){
if( reg.Match( c.ID ).Success ){
// your control is the textbox you wanted
// convert it to a textbox, get it's values...
}
}

What happens in this case is, you might have 30 textboxes created
dynamically, and when you return to your page (post-back), you will need to
determine which textbox is the one you want based on the name, and the
uniqueID. If you iterate through them, you can determine this. I hope I did
not confuse you.
--
Alex
Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Will | last post: by
3 posts views Thread by Leo J. Hart IV | last post: by
reply views Thread by pbb | last post: by
1 post views Thread by Diffident | last post: by
9 posts views Thread by Tarscher | last post: by
reply views Thread by Eniac | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.