Prevent loading of ContentPlaceHolder from MasterPage

136 Views Asked by At

I have a masterpage, contentplaceholder and an .ascx page. The user enters his username-password at Masterpage.

I want to prevent the load of the contentplaceholder, if the user enters wrong username&password combination. Currently I am just disabling it's visibility, which does the trick but the page is still loaded, goes to database etc. which is useless since all of them will not be shown anyway.

1

There are 1 best solutions below

0
VDWWD On

You can load the Controls dynamically.

private WebUserControl1 userControl;

protected void Button1_Click(object sender, EventArgs e)
{
    if (loginOK == true)
    {
        buildControls();
    }
}

private void buildControls()
{
    userControl = (WebUserControl1)LoadControl("~/WebUserControl1.ascx");
    PlaceHolder1.Controls.Add(userControl);
}

Dynamically added controls need to be recreated on every Page_load (that includes PostBack). So always call buildControls() when a user is logged in.