Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: User Control Postback State Question Answered


Message #1 by "Scott Guthrie" <scottgu@m...> on Thu, 22 Mar 2001 13:49:31 -0800
Origional Question:

-----------------------------------



I have an .ASCX file that gets loaded by a parent .ASPX page using

Page.LoadControl. The .ASCX file contains a drop down list control that

has maintainstate=3Dtrue set. However on my .ASPX page that has other 

form



elements that fire various events...when these events fire (and the page



reloads) my form elements in the user control (.ASCX) get reset. I guess

I

have to programmatically maintain state for those elements.

Suggestions!?



Thanks!



--Brandon



ScottGu Answer:

-----------------------------------



Hi Brandon,



The problem you are running into is that you are calling

Page.LoadControl and adding your .ASCX User Control into the page

*after* the postback process has occured (this happens right before the

Page_Load event fires).  As such, the posted back values and viewstate

do not get applied to the controls within your User Control (since they

miss this process).



To fix this, try calling Page.LoadControl and adding your .ASCX User

Control within the Page_Init event instead.  This event happens *before*

the postback process occurs.  Adding your user control into the tree at

this point will ensure that the appropriate values/state get posted

back.



<html>



    <script language=3D"VB" runat=3Dserver>



        Sub Page_Init(Sender as Object, E as EventArgs)





MyPanel.Controls.Add(Page.LoadControl("MyUserControl.ascx"))



        End Sub



    </script>



    <body>

        <asp:panel id=3D"MyPanel" runat=3Dserver/>

    </body>

</html>



Hope this helps,



Scott

Message #2 by "Brandon Bohling" <brandon.bohling@i...> on Thu, 22 Mar 2001 22:55:20
Scott--



The one problem with the solution you provided is that at the time 

of "Page_Init" I do not know *which* user control I am going to add 

(totally dynamic). So, I am calling Page.LoadControl via the 

OnSelection_Event. Any other suggestions? Oh, and btw...this is a nested 

control as well. Which means I have an .ASPX page that loads in the 

main .ASCX page, and that user control loads "helper" user controls files. 

All this complexity so I have a "clever" report form (for web 

metrics)...anytime a customer wants a new report, i simply update an xml 

file and shazam...it is automatically part of the report form. woohoo! but 

a pain developing! hehe



--brandon





> Origional Question:

> -----------------------------------

> 

> I have an .ASCX file that gets loaded by a parent .ASPX page using

> Page.LoadControl. The .ASCX file contains a drop down list control 

that

> has maintainstate=3Dtrue set. However on my .ASPX page that has other 

> form

> 

> elements that fire various events...when these events fire (and the page

> 

> reloads) my form elements in the user control (.ASCX) get reset. I guess

> I

> have to programmatically maintain state for those elements.

> Suggestions!?

> 

> Thanks!

> 

> --Brandon

> 

> ScottGu Answer:

> -----------------------------------

> 

> Hi Brandon,

> 

> The problem you are running into is that you are calling

> Page.LoadControl and adding your .ASCX User Control into the page

> *after* the postback process has occured (this happens right before the

> Page_Load event fires).  As such, the posted back values and viewstate

> do not get applied to the controls within your User Control (since they

> miss this process).

> 

> To fix this, try calling Page.LoadControl and adding your .ASCX User

> Control within the Page_Init event instead.  This event happens *before*

> the postback process occurs.  Adding your user control into the tree at

> this point will ensure that the appropriate values/state get posted

> back.

> 

> <html>

> 

>     <script language=3D"VB" runat=3Dserver>

> 

>         Sub Page_Init(Sender as Object, E as EventArgs)

> 

> 

> MyPanel.Controls.Add(Page.LoadControl("MyUserControl.ascx"))

> 

>         End Sub

> 

>     </script>

> 

>     <body>

>         <asp:panel id=3D"MyPanel" runat=3Dserver/>

>     </body>

> </html>

> 

> Hope this helps,

> 

> Scott

Message #3 by "Richard Anderson" <rja@a...> on Sat, 24 Mar 2001 19:11:13 -0000
I didnt see a reply to this question (if there was ignore this one!)...



As controls are created in a page (such as custom web controls, user

controls etc) they are eventually added to the Controls collections of a

container (eg. the page, a user control etc).  When this happens, controls

'catch up' on their events.  So, in theory, if you dynamically create a

control in your select event, when that control is added to a controls

collection of the container, the loadviewstate method will kick in, so your

state should be their.



Is there any way you can send me a simple repro of your problem ?



Cheers,



Rich.





----- Original Message -----

From: "Brandon Bohling" <brandon.bohling@i...>

To: "ASP+" <aspx@p...>

Sent: Thursday, March 22, 2001 10:55 PM

Subject: [aspx] Re: User Control Postback State Question Answered





> Scott--

>

> The one problem with the solution you provided is that at the time

> of "Page_Init" I do not know *which* user control I am going to add

> (totally dynamic). So, I am calling Page.LoadControl via the

> OnSelection_Event. Any other suggestions? Oh, and btw...this is a nested

> control as well. Which means I have an .ASPX page that loads in the

> main .ASCX page, and that user control loads "helper" user controls files.

> All this complexity so I have a "clever" report form (for web

> metrics)...anytime a customer wants a new report, i simply update an xml

> file and shazam...it is automatically part of the report form. woohoo! but

> a pain developing! hehe

>

> --brandon

>

>

> > Origional Question:

> > -----------------------------------

> >

> > I have an .ASCX file that gets loaded by a parent .ASPX page using

> > Page.LoadControl. The .ASCX file contains a drop down list control

> that

> > has maintainstate=3Dtrue set. However on my .ASPX page that has other 

> > form

> >

> > elements that fire various events...when these events fire (and the page

> >

> > reloads) my form elements in the user control (.ASCX) get reset. I guess

> > I

> > have to programmatically maintain state for those elements.

> > Suggestions!?

> >

> > Thanks!

> >

> > --Brandon

> >

> > ScottGu Answer:

> > -----------------------------------

> >

> > Hi Brandon,

> >

> > The problem you are running into is that you are calling

> > Page.LoadControl and adding your .ASCX User Control into the page

> > *after* the postback process has occured (this happens right before the

> > Page_Load event fires).  As such, the posted back values and viewstate

> > do not get applied to the controls within your User Control (since they

> > miss this process).

> >

> > To fix this, try calling Page.LoadControl and adding your .ASCX User

> > Control within the Page_Init event instead.  This event happens *before*

> > the postback process occurs.  Adding your user control into the tree at

> > this point will ensure that the appropriate values/state get posted

> > back.

> >

> > <html>

> >

> >     <script language=3D"VB" runat=3Dserver>

> >

> >         Sub Page_Init(Sender as Object, E as EventArgs)

> >

> > 

> > MyPanel.Controls.Add(Page.LoadControl("MyUserControl.ascx"))

> >

> >         End Sub

> >

> >     </script>

> >

> >     <body>

> >         <asp:panel id=3D"MyPanel" runat=3Dserver/>

> >     </body>

> > </html>

> >

> > Hope this helps,

> >

> > Scott

>

  Return to Index