views:

197

answers:

3

I have a C#/ASP.NET .aspx page that declares two controls that each represents the content of one tab. I want a query string argument (e.g., ?tab=1) to determine which of the two controls is activated. My problem is, they both go through the initialization events and populate their child controls, wasting CPU resources and slowing the response time. Is it possible to deactivate them somehow so they don't go through any initialization?

My .aspx page looks like this:

<% if (TabId == 0)
   { %>
<my:usercontroltabone id="ctrl1" runat="server" />
<% }
   else if (TabId == 1)
   { %>
<my:usercontroltabtwo id="ctrl2" runat="server" />
<% } %>

And that part works fine. I assumed the that <%'s would have meant the control wouldn't actually be declared and so wouldn't initialize, but that isn't so...

+1  A: 

move the initialization code out of the .Load and into your own custom public method, and call the initiator explicitly when appropriate.

Mike Jacobs
+5  A: 

If inline/spaghetti server side code does not help, I can only think of one alternative solution: avoid declaring the controls in the markup. Instead, load the control you actually want on the page from your Page_Init event handler. The Page.LoadControl() method can be used for this:

void Page_Init(object sender, System.EventArgs e)
{
    Control tab;

    switch (TabId)
    {
        case 0: tab = LoadControl("usercontroltabone.ascx"); break;
        case 1: tab = LoadControl("usercontroltabtwo.ascx"); break;
        default: tab = LoadControl("defaulttab.ascx"); break;
    }

    somePlaceholder.Controls.Add(tab);
}
Jørn Schou-Rode
I agree that the spaghetti code needs to go. Logic should remain in the codebehind to keep things object-oriented.
hypoxide
This way works good, the caveat is if you are depending on viewstate. Viewstate is not loaded automagically on dynamically added controls.
Chuck Conway
@Chuck: If my memory serves me right (it's been some time since I worked much with web forms) that really depends on *when* the controls are loaded. I believe that loading in `Page_Init` rather than `Page_Load` solves (most of) the viewstate issues.
Jørn Schou-Rode
@Jørn Schou-Rode: Your memory serves you well, see **Basic OF .NET** towards the bottom of the page for order in which events are fired. http://msdn.microsoft.com/en-us/library/ms178472.aspx. Relevant section:Page: Init | Page: TrackViewState |Page: InitComplete
R0MANARMY
A: 

Apply your logic in the codebehind.

Declare the control:

<my:usercontroltabtwo id="ctrl2" Visible="False" runat="server" />

And then set the Visibility:

if(TabId == 0)
{
ctrl1.Visible = true;
}
else if(TabId == 1)
{

ctrl2.Visible = true;
}
Chuck Conway
This will not help.
SLaks
@Chuck: That's basically what I was doing, but ctrl1 and ctrl2 (and all their child controls) all got the startup events anyway, which is what I'm trying to avoid.
Scott Stafford
Adding to SLaks' comment: This approach will only cause the control not to *render*, but it will still be loaded into and initialized in the control tree. Essentially, this is what the OP already has.
Jørn Schou-Rode