Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Using IsPostBack in Page_Load of dynamically added control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DC - 30 Nov 2007 15:00 GMT
Hi,

I am dynamically adding a usercontrol that uses "this.IsPostBack" in
Page_Load to decide whether it must populate some of it's inner
controls or not. Since I am adding the usercontrol to the page at
runtime (on response to a click event), IsPostBack is always false and
the inner controls never get populated.

I changed "IsPostBack" to "IsInitial" now, using this property:

       public bool IsInitial
       {
           get
           {
               if (ViewState["IsInitial"] == null)
               {
                   ViewState["IsInitial"] = false;
                   return true;
               }
               return false;
           }
       }

I don't think this is the way to do it. If the sourcecode of the
control is not available, I cannot add the usercontrol dynamically
without running into this problem. Is there a way to explicitly set
"IsPostBack" of the control to "true" from outside somehow? I thought
using "if (!IsPostBack)" to init controls in Page_Load is recommended.

Kind regards
DC
Scott Roberts - 30 Nov 2007 15:23 GMT
I believe that dynamically added controls must be created in Page_Init in
order to have their properties set automatically from the viewstate. I'd
image the IsPostBack property works the same way.

One option is to add the user control at design time then simply show/hide
it at runtime. Another option is to always add the user control in Page_Init
then remove it (in, say, PreRender) if it is not ultimately needed. Finally,
you could keep your "IsInitial" viewstate property but check it in Page_Init
and create the user control if it is true (and maybe rename it to
"LoadUserControl").

> Hi,
>
[quoted text clipped - 27 lines]
> Kind regards
> DC
DC - 03 Dec 2007 08:26 GMT
On 30 Nov., 16:23, "Scott Roberts" <srobe...@no.spam.here-webworks-
software.com> wrote:
> I believe that dynamically added controls must be created in Page_Init in
> order to have their properties set automatically from the viewstate. I'd
[quoted text clipped - 40 lines]
>
> - Zitierten Text anzeigen -

Thank you, Scott!

I tried loading the control in OnInit but the only difference I got is
that I cannot do the loading depending on some other controls that
depend from viewstate (since they will not have their viewstate
processed yet). The dynamically loaded control does still always have
"IsPostBack == true".

I am quiet convinced that the design of my application is flawed. What
I want to design is a MultiView with rather large fat controls in
every view. I wanted to avoid that all the applications in all the
views execute Page_Load on all PostBacks - although they are actually
hidden.

So I thought it was smart to dynamically load the control only when
the actual View of the control was the active view. Of course, the
control that I would then dynamically load into the View must have
"IsPostBack == false" on the initial load on "IsPostBack == true" on
subsequent loads of the same View.

So what I want is like a page in a page, kind of like I was loading
the control in the view into an Iframe and have it live its private
life. Once another View is activated, the dynamically loaded control
is allowed to loose its state.

But I think that's not a good design in ASP.Net since the architecture
relies on one state for the whole page (it is a similar problem like
the "one serverside form per page only" limitation). So I am better of
to plant all the controls into their views at designtime and get the
advantage of retaining their state (if that is desired). I will do
that now and measure the actual performance hit.

Regards
DC

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.