Problem getting all controls from the current page

A

antonyliu2002

I am new to the .NET framework.

I know this has been discussed many times in this group. I also read
extensively here, however, I am in bad luck: none of the sample code
provided in this forum worked for me.

What I want to do is really simple.

I simply want to iterate through all controls of the current page and
create session objects for TextBoxes and CheckBoxes.

I tried this:

Sub CreateSessions()
Dim ctlObj As System.Web.UI.WebControls.WebControl
For Each ctlObj in Me.Controls
If ctlObj.GetType().ToString() =
"System.Web.UI.WebControls.TextBox" Then
Session(ctlObj.ToString()) = ctlObj.Text
End If

If ctlObj.GetType().ToString() =
"System.Web.UI.WebControls.CheckBox" Then
Session(ctlObj.ToString()) = ctlObj.Checked
End If
Next
End Sub

The error message I got:

Compiler Error Message: BC30456: 'Text' is not a member of
'System.Web.UI.WebControls.WebControl'.

Then what type should I assign to ctlObj? Thanks a lot!
 
K

Karl Seguin

Dim ctlObj As System.Web.UI.Control
For Each ctlObj In Me.Controls
If TypeOf ctlObj Is TextBox Then
Session(ctlObj.ToString()) = CType(ctlObj, TextBox).Text
ElseIf TypeOf ctlObj Is CheckBox Then
Session(ctlObj.ToString()) = CType(ctlObj, CheckBox).Checked
End If
Next

First, ctlObj should be of type Control not WebControl 'cuz you never know
what type of controls are gonna be in your page.

2nd, use typeof xx Is YY instead of the GetType().ToString()

finally, and the reason it isn't working, in order to access the Text
property of your textbox, you need to cast your ctlObj to a textbox. Even
though it's of type TextBox, it's actually a WebControl (Control in my
example) object, which doesn't have a TExt property

Karl
 
P

Patrice

You should cast the WebControl to a TextBox. The Text property is not
available for a high level "WebControl". It is only provided by more
specialized inheritors such as the TextBox.

Also keep in mind that the controls are in a tree. You should recurse inside
each controls collection else you'll see only those who are at the very
first level.

You could time how much time it takes and see if it's worth depending on
what you are trying to do (especially if the forms doesn't contains any
dynamic control) you could just do something like :

MyControls=Array(Text1,Text2,DropDownList2)
CreateSessions(MyControls)

Or also :
- see if the control supports IPostBackDataHandler. This interface is
implemented by all controls that are able to post values (including also
dropdownlist etc...).
- still another way would be to browse the Request.Form collection that only
contains posted values (likely quicker)...

Post perhaps also about what you are trying to do...
 
A

antonyliu2002

Hey, thank you very much for your great hint! Actually, Request.Form
sounds like what I want. I did not know about this before. But I think
I did Request.Form before in Java Servlets, so it sounds familiar to
me.

Thanks again. Let me try to figure out how to use Request.Form.
 
A

antonyliu2002

Hi, thanks a lot! It's been years since I last programmed in Java,
which does the same kind of casting type thing. You got the problem
corrected. Thanks again.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top