How to declare C # web user control but stop it from initializing?

I have a C # / ASP.NET.aspx page that declares two controls, each of which represents the contents 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 that they both go through initialization events and populate their child controls, losing CPU resources and slowing down response time. Is it possible to somehow deactivate them so that they do not 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 this part works great. I assumed that <% would mean that the control would not actually be declared and therefore would not be initialized, but it is not ...

+5
4

inline/spaghetti , : . , , Page_Init. Page.LoadControl():

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);
}
+7

.Load , .

+1

.

:

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

:

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

ctrl2.Visible = true;
}
0

UserControl.dispose() usercontrol page_load .

0

All Articles