473,785 Members | 2,737 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("Sessio n1") = ctrl1.Text
Session("Sessio n2") = ctrl2.Text
Session("Sessio n3") = ctrl3.Text
Session("Sessio n4") = ctrl4.SelectedV alue
Session("Sessio n5") = ctrl5.Text
Session("Sessio n6") = 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.FindContro l("MyForm").Con trols

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 3336
an***********@y ahoo.com wrote in news:1128447022 .689767.319880
@o13g2000cwo.go oglegroups.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.FindContro l("MyForm").Con trols

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********@rog ers.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).S electedValue
else if typeof child is ... ' youneed to do this for every type
..
end if

if child.HasContro l 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...whatev er 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.goo glegroups.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("Sessio n1") = ctrl1.Text
Session("Sessio n2") = ctrl2.Text
Session("Sessio n3") = ctrl3.Text
Session("Sessio n4") = ctrl4.SelectedV alue
Session("Sessio n5") = ctrl5.Text
Session("Sessio n6") = 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.FindContro l("MyForm").Con trols

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).S electedValue
else if typeof child is ... ' youneed to do this for every type
..
end if

if child.HasContro l 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...whatev er 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.goo glegroups.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("Sessio n1") = ctrl1.Text
Session("Sessio n2") = ctrl2.Text
Session("Sessio n3") = ctrl3.Text
Session("Sessio n4") = ctrl4.SelectedV alue
Session("Sessio n5") = ctrl5.Text
Session("Sessio n6") = 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.FindContro l("MyForm").Con trols

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_Clicke d
dim user as new User()
user.Name = UserName.Text
user.Email= Email.Text
user.Country= Country.Selecte dValue
Session("NewUse r") = user
Response.Redire ct("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.Redire ct("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.goo glegroups.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).S electedValue
else if typeof child is ... ' youneed to do this for every type
..
end if

if child.HasContro l 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...whatev er 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.goo glegroups.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("Sessio n1") = ctrl1.Text
> Session("Sessio n2") = ctrl2.Text
> Session("Sessio n3") = ctrl3.Text
> Session("Sessio n4") = ctrl4.SelectedV alue
> Session("Sessio n5") = ctrl5.Text
> Session("Sessio n6") = 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.FindContro l("MyForm").Con trols
>
> 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_Clicke d
dim user as new User()
user.Name = UserName.Text
user.Email= Email.Text
user.Country= Country.Selecte dValue
Session("NewUse r") = user
Response.Redire ct("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.Redire ct("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.goo glegroups.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).S electedValue
else if typeof child is ... ' youneed to do this for every type
..
end if

if child.HasContro l 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...whatev er 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.goo glegroups.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("Sessio n1") = ctrl1.Text
> Session("Sessio n2") = ctrl2.Text
> Session("Sessio n3") = ctrl3.Text
> Session("Sessio n4") = ctrl4.SelectedV alue
> Session("Sessio n5") = ctrl5.Text
> Session("Sessio n6") = 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.FindContro l("MyForm").Con trols
>
> 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.A dd("NewUser", user)
Server.Transfer ("Page2.aspx ")
and then you can retrieve the user from Page2.aspx ala:
dim user as User = ctype(Context.I tems("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.goo glegroups.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_Clicke d
dim user as new User()
user.Name = UserName.Text
user.Email= Email.Text
user.Country= Country.Selecte dValue
Session("NewUse r") = user
Response.Redire ct("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.Redire ct("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.goo glegroups.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).S electedValue
>> else if typeof child is ... ' youneed to do this for every type
>> ..
>> end if
>>
>> if child.HasContro l 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...whatev er 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.goo glegroups.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("Sessio n1") = ctrl1.Text
>> > Session("Sessio n2") = ctrl2.Text
>> > Session("Sessio n3") = ctrl3.Text
>> > Session("Sessio n4") = ctrl4.SelectedV alue
>> > Session("Sessio n5") = ctrl5.Text
>> > Session("Sessio n6") = 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.FindContro l("MyForm").Con trols
>> >
>> > 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.A dd("NewUser", user)
Server.Transfer ("Page2.aspx ")
and then you can retrieve the user from Page2.aspx ala:
dim user as User = ctype(Context.I tems("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.goo glegroups.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_Clicke d
dim user as new User()
user.Name = UserName.Text
user.Email= Email.Text
user.Country= Country.Selecte dValue
Session("NewUse r") = user
Response.Redire ct("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.Redire ct("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.goo glegroups.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).S electedValue
>> else if typeof child is ... ' youneed to do this for every type
>> ..
>> end if
>>
>> if child.HasContro l 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...whatev er 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.goo glegroups.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("Sessio n1") = ctrl1.Text
>> > Session("Sessio n2") = ctrl2.Text
>> > Session("Sessio n3") = ctrl3.Text
>> > Session("Sessio n4") = ctrl4.SelectedV alue
>> > Session("Sessio n5") = ctrl5.Text
>> > Session("Sessio n6") = 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.FindContro l("MyForm").Con trols
>> >
>> > 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.goo glegroups.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.A dd("NewUser", user)
Server.Transfer ("Page2.aspx ")
and then you can retrieve the user from Page2.aspx ala:
dim user as User = ctype(Context.I tems("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.goo glegroups.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
/frlrfSystemWebH ttpServerUtilit yClassTransferT opic2.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_Clicke d
> dim user as new User()
> user.Name = UserName.Text
> user.Email= Email.Text
> user.Country= Country.Selecte dValue
> Session("NewUse r") = user
> Response.Redire ct("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.Redire ct("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.goo glegroups.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).S electedValue
> >> else if typeof child is ... ' youneed to do this for every type> >> ..
> >> end if
> >>
> >> if child.HasContro l 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...whatev er 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.goo glegroups.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("Sessio n1") = ctrl1.Text
> >> > Session("Sessio n2") = ctrl2.Text
> >> > Session("Sessio n3") = ctrl3.Text
> >> > Session("Sessio n4") = ctrl4.SelectedV alue
> >> > Session("Sessio n5") = ctrl5.Text
> >> > Session("Sessio n6") = 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.FindContro l("MyForm").Con trols
> >> >
> >> > 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(sende r 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.goo glegroups.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.A dd("NewUser", user)
Server.Transfer ("Page2.aspx ")
and then you can retrieve the user from Page2.aspx ala:
dim user as User = ctype(Context.I tems("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.goo glegroups.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
/frlrfSystemWebH ttpServerUtilit yClassTransferT opic2.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_Clicke d
>> dim user as new User()
>> user.Name = UserName.Text
>> user.Email= Email.Text
>> user.Country= Country.Selecte dValue
>> Session("NewUse r") = user
>> Response.Redire ct("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.Redire ct("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.goo glegroups.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).S electedValue
>> >> else if typeof child is ... ' youneed to do this for every type >> >> ..
>> >> end if
>> >>
>> >> if child.HasContro l 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...whatev er 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.goo glegroups.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("Sessio n1") = ctrl1.Text
>> >> > Session("Sessio n2") = ctrl2.Text
>> >> > Session("Sessio n3") = ctrl3.Text
>> >> > Session("Sessio n4") = ctrl4.SelectedV alue
>> >> > Session("Sessio n5") = ctrl5.Text
>> >> > Session("Sessio n6") = 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.FindContro l("MyForm").Con trols
>> >> >
>> >> > 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
2651
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 and this causes control B to be created, when this is done the VIEWSTATE is lost for CONTROL B. In the EVENT that causes CONTROL B to be created I have to set
8
4280
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 which is related to the repeater. In the code shown below, if I call Repeater1.Controls.Count in the OnInit (the code fragment was highlighted in yellow) , the viewstate for the repeater will be lost during the postback. You can re-produce this...
1
2136
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 to programming, but I am new to ASP.NET and Web application design in general... loved the concept of user controls and dynamically adding them to a page. So what I wound up with was an application that dynamically loads two user controls directly...
10
5325
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 certain control by id, but I want to find all controls of a certain type (DropDownList in this case). Is there an easier way than to get a control count of the page, loop through all controls on that page, examine their type and, if they're a...
11
1607
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
2093
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. Each question is displayed and answered, then result written to a SQL table. Then the next question is read from a table and displayed using the load_page event again. The questions display and function perfectly. The user anwers the question...
3
2793
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 System.Web.UI.Control.BuildProfileTree(String parentId, Boolean calcViewState) +201 System.Web.UI.Control.BuildProfileTree(String parentId, Boolean calcViewState) +263
3
2346
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 page. So whenever the Admin person comes to know about the new category in the market he will be adding as different Sub-Categories for example ABAP, BDC etc..etc.. on every click event as Checkboxes. And these controls(checkboxes) should remain...
22
2193
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 stage. One approach I have used on other systems is to prevent the action buttons appearing. For example, if one did not have the Role of Administrator, one would be prevented from deleting a ticket not created by oneself. However, it did occur...
12
1601
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 custom Page object which is derived from the base Page object. We also have custom controls that derive from a base class that performs custom drawing and inherits from our own IOurControl interface. There is also a special caching layer in the mix...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4050
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 we have to send another system
3
2879
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.