472,980 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,980 software developers and data experts.

Problem getting all controls from the current page

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!

Nov 19 '05 #1
4 1347
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...
--
Patrice

<an***********@yahoo.com> a écrit dans le message de
news:11*********************@g49g2000cwa.googlegro ups.com...
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!

Nov 19 '05 #2
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

--
MY ASP.Net tutorials
http://www.openmymind.net/

<an***********@yahoo.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
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!

Nov 19 '05 #3
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.

Patrice wrote:
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...
--
Patrice

<an***********@yahoo.com> a écrit dans le message de
news:11*********************@g49g2000cwa.googlegro ups.com...
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!


Nov 19 '05 #4
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.

Karl Seguin wrote:
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

--
MY ASP.Net tutorials
http://www.openmymind.net/

<an***********@yahoo.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
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!


Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically...
3
by: George K | last post by:
Hi, i have a very irriting problem that i have written a short piece of code to demonstrate. The problem is that my aspx page is not fully refreshed after an event, which is delegated to an...
1
by: José Joye | last post by:
Hello, I'm playing around with dynamically loading user controls ...and having problems I created a really simple userControl (in fact contains a plain text box) and placed it into the...
4
by: Bass Pro | last post by:
Hi, I am creating textbox, radiobuttonlist and checkboxlist dynamically depending on data from a table. It is a questionnaire. I add the control on a Panel control during the 1st load_page event....
1
by: Kevin R | last post by:
This is one of the weirdest problems I have ever run into. I have had to trim down a bunch of code to give a sample that is more easily readable by those who will view this. Here is the problem:...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
5
by: Brent | last post by:
Ok everyone i have dealing with this problem for a week and its starting to get on my nerve. I am pretty new to the asp.net world. I am not new to .Net only asp.net. So i hate a snag. right now im...
5
by: ThunderMusic | last post by:
Hi, I always refer to this page to know the order of events in a page : http://weblogs.asp.net/jeff/archive/2004/07/04/172683.aspx but this time, I'm mystified... I have a Control called...
0
by: Syoam4ka | last post by:
My project is about jewellery. I have devided my jewelery into main types, which each one of them has sub types, and each one those sub types has the jewellery. I have a tabcontainer. It includes...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.