How to get the registered user control to code beh

2019-07-18 04:16发布

问题:

I have register a page using the code

<%@ Register TagPrefix="uc1" TagName="side" Src="side.ascx" %>

And called this by using the code

<uc1:side ID="Side1" runat="server"></uc1:side>

And i tried to acces the ID="Side1" in behind by using

private void Page_Load(object sender, System.EventArgs e)
{
    // Put user code to initialize the page here

    if (Session["UName"] != null || Session["UName"] != " ")
    {
        Side1.Visible = true;
    }
    else
        Side1.Visible = true;
    }

but gives the error

The name 'Side1' does not exist in the current context .

How will i resolve it by accessing the 'id' using 'protected System.Web.UI'.

回答1:

Check the access modifier on your WebUserControl partial class definition (ie YourControl.ascx.cs file), it should be something like this:

public partial class SideControl : System.Web.UI.UserControl
{
    // your code...
}

You can find more information about access modifiers here.