Re: How to get all web controls from a page?
- From: antonyliu2002@xxxxxxxxx
- Date: 4 Oct 2005 14:17:21 -0700
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/
> <antonyliu2002@xxxxxxxxx> wrote in message
> news:1128451577.162694.126320@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> > 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/microsoft.public.dotnet.framework.aspnet/browse_frm/thread/a556ad31b5d7acb1/7656748848a2d36d?q=antonyliu2002+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.
> >
> > 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/default.asp?url=/library/en-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/
> >> <antonyliu2002@xxxxxxxxx> wrote in message
> >> news:1128449434.731741.183270@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> >> > 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/
> >> >> <antonyliu2002@xxxxxxxxx> wrote in message
> >> >> news:1128447022.689767.319880@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> >> >> >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.
> >> >> >
> >> >
> >
.
- Follow-Ups:
- Re: How to get all web controls from a page?
- From: Patirck Ige
- Re: How to get all web controls from a page?
- Prev by Date: TreeNode subclass
- Next by Date: Re: Can /bin directory be virtual?
- Previous by thread: TreeNode subclass
- Next by thread: Re: How to get all web controls from a page?
- Index(es):
Relevant Pages
|