473,508 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get all web controls from a page?

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.

Nov 19 '05 #1
9 3319
an***********@yahoo.com wrote in news:1128447022.689767.319880
@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


--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.

Newmarket Volvo Sucks! http://newmarketvolvo.tripod.com
Nov 19 '05 #2
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
<an***********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.

Nov 19 '05 #3
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.
Karl Seguin wrote:
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
<an***********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.


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

--
MY ASP.Net tutorials
http://www.openmymind.net/
<an***********@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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.
Karl Seguin wrote:
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
<an***********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
>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.
>

Nov 19 '05 #5
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/micro...56748848a2d36d

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.

Karl Seguin wrote:
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/de...sferTopic2.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

--
MY ASP.Net tutorials
http://www.openmymind.net/
<an***********@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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.
Karl Seguin wrote:
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
<an***********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
>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.
>


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

--
MY ASP.Net tutorials
http://www.openmymind.net/
<an***********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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/micro...56748848a2d36d

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.

Karl Seguin wrote:
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/de...sferTopic2.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

--
MY ASP.Net tutorials
http://www.openmymind.net/
<an***********@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
> 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.
>
>
> Karl Seguin wrote:
>> 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
>>
>> --
>> MY ASP.Net tutorials
>> http://www.openmymind.net/
>> <an***********@yahoo.com> wrote in message
>> news:11**********************@o13g2000cwo.googlegr oups.com...
>> >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.
>> >
>

Nov 19 '05 #7
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.
Karl Seguin wrote:
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
<an***********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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/micro...56748848a2d36d

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.

Karl Seguin wrote:
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/de...sferTopic2.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

--
MY ASP.Net tutorials
http://www.openmymind.net/
<an***********@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
> 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.
>
>
> Karl Seguin wrote:
>> 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
>>
>> --
>> MY ASP.Net tutorials
>> http://www.openmymind.net/
>> <an***********@yahoo.com> wrote in message
>> news:11**********************@o13g2000cwo.googlegr oups.com...
>> >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.
>> >
>


Nov 19 '05 #8
Antony there is a clean example here at:-
http://aspnet101.com/aspnet101/tutorials.aspx?id=20
Hope tha helps
Patrick

<an***********@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.
Karl Seguin wrote:
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
<an***********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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/micro...56748848a2d36d
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.

Karl Seguin wrote:
> 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/de...-us/cpref/html
/frlrfSystemWebHttpServerUtilityClassTransferTopic2 .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
>
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/
> <an***********@yahoo.com> wrote in message
> news:11**********************@g47g2000cwa.googlegr oups.com...
> > 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.
> >
> >
> > Karl Seguin wrote:
> >> 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
> >>
> >> --
> >> MY ASP.Net tutorials
> >> http://www.openmymind.net/
> >> <an***********@yahoo.com> wrote in message
> >> news:11**********************@o13g2000cwo.googlegr oups.com...
> >> >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.
> >> >
> >

Nov 19 '05 #9
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.
Patirck Ige wrote:
Antony there is a clean example here at:-
http://aspnet101.com/aspnet101/tutorials.aspx?id=20
Hope tha helps
Patrick

<an***********@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.
Karl Seguin wrote:
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
<an***********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
> 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/micro...56748848a2d36d >
> 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.
>
> Karl Seguin wrote:
>> 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/de...-us/cpref/html
/frlrfSystemWebHttpServerUtilityClassTransferTopic2 .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
>>
>> --
>> MY ASP.Net tutorials
>> http://www.openmymind.net/
>> <an***********@yahoo.com> wrote in message
>> news:11**********************@g47g2000cwa.googlegr oups.com...
>> > 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.
>> >
>> >
>> > Karl Seguin wrote:
>> >> 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
>> >>
>> >> --
>> >> MY ASP.Net tutorials
>> >> http://www.openmymind.net/
>> >> <an***********@yahoo.com> wrote in message
>> >> news:11**********************@o13g2000cwo.googlegr oups.com...
>> >> >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.
>> >> >
>> >
>


Nov 19 '05 #10

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

Similar topics

3
2629
by: Steve Drake | last post by:
All, I have a CONTROL that contains 1 control (Control ONE), the 1 control that it can contain 1 or 2 control (Control A and B). Control A, raises and event and Control ONE receives this event...
8
4265
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue...
1
2116
by: Robert Howells | last post by:
Perhaps I'm just too new at this to pull it off, or perhaps it's just bad architecture. I'd appreciate some feedback on the the wisdom (or lack thereof) in attempting the following: I'm not new...
10
5286
by: Sacha Korell | last post by:
I'm trying to load a drop-down list with all DropDownList control names from another page. How would I be able to find those DropDownList controls? The FindControl method will only find a...
11
1582
by: Frank Esser | last post by:
Hi, I created an ASP.NET page (test.aspx) with some web controls in GridLayout (Design time). When I look at the collection page.controls by foreach (Control ctrl in Page.Controls) { ....
4
2077
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....
3
2769
by: Nathan Sokalski | last post by:
When I view any page in my application a second time, I recieve the following error: System.Web.TraceContext.AddNewControl(String id, String parentId, String type, Int32 viewStateSize) +313...
3
2327
by: Ankit Aneja | last post by:
I have a strange situation and I have no idea how to solve this. Its a Recruitment Search Page,in the Admin Page, for every button click event the Admin Person has to create a checkbox on the users...
22
2149
by: Mr Newbie | last post by:
I was thinking about developing a workflow application yesterday and was musing over the different approaches than one could take in restricting specific actions on a ticket( Form ) at any said...
12
1584
by: Bob Jones | last post by:
I have an odd business requirement and I think that the implementation is not correct in the terms of OOP development. Any help on the concepts would be very appreciated! We currently have a...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.