How to get all web controls from a page?

A

antonyliu2002

I want to create session objects for all web controls in a page. Right
now, I am doing it in a dumb way like this (for example):

Session("Session1") = ctrl1.Text
Session("Session2") = ctrl2.Text
Session("Session3") = ctrl3.Text
Session("Session4") = ctrl4.SelectedValue
Session("Session5") = ctrl5.Text
Session("Session6") = ctrl6.Text

It would be good if I can get all web controls and loop through them.
I searched this group and noticed that this topic has been discussed in
a few threads. But I've tried

For Each objCtrl in Page.FindControl("MyForm").Controls

and

For Each objCtrl In Page.Controls

and

For Each objCtrl In MyForm.Controls

and

For Each objCtrl In Me.Controls

None of them worked. Would any guru please share your idea? Thanks.
 
L

Lucas Tam

(e-mail address removed) wrote in @o13g2000cwo.googlegroups.com:
It would be good if I can get all web controls and loop through them.
I searched this group and noticed that this topic has been discussed in
a few threads. But I've tried

For Each objCtrl in Page.FindControl("MyForm").Controls

and

For Each objCtrl In Page.Controls

and

For Each objCtrl In MyForm.Controls

and

For Each objCtrl In Me.Controls

None of them worked. Would any guru please share your idea? Thanks.

The above DO work - but what you have to do is create a recursive
function to loop through all the controls. This is because controls on
the page are nested:

Page Control
PlaceHolder Control
Table Control
Row Control
Cell Control
MY TEXT BOX CONTROL
 
K

Karl Seguin

I'm cautious about given any help 'cuz your approach seems mad! Why in the
world would you ever want to do what you are doing?! We'll, I'll assume
you've thought it trough *shrug*.

public sub LoadIntoSession(byval parent as control, byref count as integer)
for each child as Control in Parent.Controls
count = count + 1
dim sessionKey as string = "Session" + count.ToString()
dim value as string
if typeof child is TextBox then
value = ctype(child, TextBox).Text
else if typeof child is ListControl then
value = ctype(child, DropDownList).SelectedValue
else if typeof child is ... ' youneed to do this for every type
..
end if

if child.HasControl then
LoadIntoSession(child, count)
end if
next
end sub


Page_Load
LoadIntoSession(Page, 0)
end sub


That code isn't perfect, but something like that should help...

I still think it's probably a bad design...whatever you are doing....
Server.Transfer w/preserve Form might work, or using the HttpContext....

Karl
 
A

antonyliu2002

First of all, thanks a lot for the sample code.

Secondly, do you mean that it is not a good idea to create a session
object for each web control?

I don't know if this is a good design or not, but my boss wants the
user-supplied data in web forms across multiple pages to be retained
while they jump around these pages.

So, I think that session is the best solution. If looping through all
web controls is troublesome, I'll just do it the dumb way: create a
session line after another.
 
K

Karl Seguin

Well, it's just going to lead to a cumbersome and complex design, in my
opinion.

Either use Server.Transfer and PreserveForm
(http://msdn.microsoft.com/library/d...emWebHttpServerUtilityClassTransferTopic2.asp)
which will maintain REquest.Form so atleast it's only 1/2 as hackish...or
better yet...

create a business object and passthat in your session.

Say you are building a user registration thing with 3 pages, page 1 : basic
information, page 2: address, page 3: membership details

Page 1 -->

sub btnPage2_Clicked
dim user as new User()
user.Name = UserName.Text
user.Email= Email.Text
user.Country= Country.SelectedValue
Session("NewUser") = user
Response.Redirect("Page2.aspx")
end sub

Page 2 -->
dim user as User = ctype(Session("NewUser"), User)
if user is nothing then
'error, has to start on page 1
end if
user.Telephone = telephone.TExt
...
..
Response.Redirect("Page2.aspx")
end sub

or you could just store the user in Context.ITems and use a Server.Transfer.

Anyways, it's similar to what you are doing, but it's better encapsulated.
You aren't passing 100 pieces of free floating information around, but
rather passing a single object and building it up....assuming that's
applicable to what you are doing.

Karl
 
A

antonyliu2002

Hi,

Thanks a lot. Sounds like that is a much better way than session to
achieve my goal. However, I still don't know anything about that yet.


Actually, a few days ago, I asked in this group if there is a better
way than session, here:

http://groups.google.com/group/micr...liu2002+session&rnum=4&hl=en#7656748848a2d36d

The MSDN link is helpful, but I cannot jumpstart from that brief doc.
Could you point me to some more detailed documentation about how to
implement this?

Thanks.
 
K

Karl Seguin

Well, if you only need the data to exist while a user hits next> next> next>
or <prev <prev <prev, you could do:

dim user as new User()
.....
Context.Items.Add("NewUser", user)
Server.Transfer("Page2.aspx")


and then you can retrieve the user from Page2.aspx ala:
dim user as User = ctype(Context.Items("NewUser", User")
'don't forget to check for nothing..

Karl
 
A

antonyliu2002

Hmm, that looks great. Looks like this way I am liberated from the
tedious session creation and retrieval task. I will definitely try to
test this. You might wanna put a tutorial about this on your web.
 
A

antonyliu2002

Hey, Patrick,

That is very helpful. I gave it a shot and it partially works, but
there is a slight problem which I don't know how to fix.

On page1.aspx, I have

Page1 | Page2 |

First Name:
Last Name:

Where Page1 and Page2 are LinkButton objects that point to the
respective pages. These two buttons also appear on page2.aspx.

On page2.aspx, I do get the info transferred from page1.aspx. But when
I hit the LinkButton Page1 to go back, the information is lost. And on
page1.aspx, I do have the following Sub:

Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
txtFirst.Text = Context.Items("first")
txtLast.Text = Context.Items("last")
End If
End Sub

Presumably, this sub should be able to retrieve the values from the
context and put them back to their respective textboxes.

Well, I know that the values will still be there if I hit the back
button of the browser. But that is not what I want.

Please let me know where I went wrong. Thanks a lot.
 

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top