Form Field Type

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello Friends

I have written a simple HTML page in aspx
I have added form elements like "textBox", "Hidden fields", "images", "radio buttons" to the html form

I am posting the data back to the same page
Is there a way to know the "Type" of a form element upon post back. Meaning in the code behind file of the page, is there a way to find the "Type" all the form elements ("textBox", "Hidden fields", "images", "radio buttons" etc

Any guidance will be greatly appreciated

Many Thanks
Mohit
 
I'm not quite sure what you're trying to do - do you want to be able to find
out the types because you are planning to have some page for which you don't
know the controls in advance?

All controls ("form elements" as you call them) which are set with the
attribute "runat=server" will appear in the Page's Controls collection (of
type ControlCollection). You can loop through this collection and use
GetType() on each control to give you the type of the control (it gives you
a Type object, but you can use ToString() on this to get a string with the
name of the type). This only applies to those which you have set with the
attribute above - any HTML controls which you add for which you don't add
this attribute won't be in the Controls collection.

Does that help at all? -is that the kind of thing you want to do?

Pete Beech


Mohit Gupta said:
Hello Friends,

I have written a simple HTML page in aspx.
I have added form elements like "textBox", "Hidden fields", "images",
"radio buttons" to the html form.
I am posting the data back to the same page.
Is there a way to know the "Type" of a form element upon post back.
Meaning in the code behind file of the page, is there a way to find the
"Type" all the form elements ("textBox", "Hidden fields", "images", "radio
buttons" etc)
 
Thanks for your reply Pete

Actually this is not what i am looking for
The controls are not server side controls, they are plain HTML 4.0 controls. So i cannot use the controlCollection
Is there a way to get the type of HTML 4.0 elements (<input type=hidden name=mycontrol> ...)?

Thank You

Mohit
 
No. In a POST Request, the form fields are represented by their names and
their values. Only.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Mohit Gupta said:
Thanks for your reply Pete!

Actually this is not what i am looking for.
The controls are not server side controls, they are plain HTML 4.0
controls. So i cannot use the controlCollection.
 
The answer to your question is: No. The posting of form data is a HTTP
specification and it supplies you with the name-value pairings when a HTTP
Request of type post is initiated.

You could prefix your controls with some meaningful naming convention, i.e.
"txt" for text inputs, "ddl" for pull down menus (selects), etc. and then
iterate through the ASP.NET Request/form collection.

Like the 1st reply-er mentioned, it is not clear why you would need to go
through all this trouble to begin with other than raising the suspicion of
one asking for help on how to hack a website. I'm sure that's not the case,
but you still did not address such in your follow-up reply.
 
You could make your plain HTML controls run on the server as well, by
including the runat=server attribute for each one - I guess they will still
be added to the Controls collection, though I've never tested that - I don't
think HTML Server Controls will behave differently to Web controls with
respect to being in the Controls collection (You can see the Control
collection by turning tracing on for the page, so you could very quickly
test this). Is that a possibility?

Otherwise, you can probably iterate through all the elements in your <form>
using javascript and the DOM (I don't know so much about javascript) - I'm
not quite sure what the best way would be to get that info to the server
though, which is where I presume you need it.

Cheers,
Pete Beech

Mohit Gupta said:
Thanks for your reply Pete!

Actually this is not what i am looking for.
The controls are not server side controls, they are plain HTML 4.0
controls. So i cannot use the controlCollection.
 
Back
Top