Iterating through a form...

J

John Buchmann

I thought this would not be difficult, but i'm stumped!

I need to iterate through a bunch of form fields, and
read the data (value, text, etc.) from them.

(I need to iterate through them programmatically because
it is not known ahead of time which form fields will be
displayed on the browser. The fields will be displayed
or hidden depending on what the user just clicked.)

So, on my page, i surrounded my .NET form field controls
inside an <asp:panel id="Pnl_Container" runat="server">
control. The reasoning is that i could then access the
collection of controls inside of it.

Sub Button_Onclick(obj as Object, e as EventArgs)
dim i as int32
for i = 0 to Pnl_Container.Controls.count - 1
Response.Write(Pnl_Container.Controls.Item(i).id
& "<br>")
Next
End Sub

I can successfully write out the "id" of each 1st level
control. But if a control is a text box, how do I write
out the text inside of it? Or if it is a radio button,
how do i get the "value"? I'm stumped. Please help! :)

Thanks,
John
 
K

Karl Seguin

At the risk of getting shot by my fellow helpers, have you considered simply
looping through the form collection?

for each key as string in Request.Form.Keys
dim value as string = Request.Form[key]
next

problem is that Key will be the ClientId...but you can deal with that.

Alternatively, you'll need to recurse all children of your panel...

Karl
 
B

bruce barker

you need to check the type of the control, and use a cast to access the
properties you want. you could use vb late binding if performance isn't an
issue.

-- bruce (sqlwork.com)


| I thought this would not be difficult, but i'm stumped!
|
| I need to iterate through a bunch of form fields, and
| read the data (value, text, etc.) from them.
|
| (I need to iterate through them programmatically because
| it is not known ahead of time which form fields will be
| displayed on the browser. The fields will be displayed
| or hidden depending on what the user just clicked.)
|
| So, on my page, i surrounded my .NET form field controls
| inside an <asp:panel id="Pnl_Container" runat="server">
| control. The reasoning is that i could then access the
| collection of controls inside of it.
|
| Sub Button_Onclick(obj as Object, e as EventArgs)
| dim i as int32
| for i = 0 to Pnl_Container.Controls.count - 1
| Response.Write(Pnl_Container.Controls.Item(i).id
| & "<br>")
| Next
| End Sub
|
| I can successfully write out the "id" of each 1st level
| control. But if a control is a text box, how do I write
| out the text inside of it? Or if it is a radio button,
| how do i get the "value"? I'm stumped. Please help! :)
|
| Thanks,
| John
|
 
G

Guest

Thanks for the help everyone. I finally got my problem
fixed. The article on looping through the controls
helped alot!

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top