简体   繁体   中英

Function of user-control type in a master page

I have a user control file called lcont.ascx . This lcont has been dragged into the master page.

Now I want to make a public function of type lcont in the master page, but masterpage.cs does not recognize lcont (saying `the type or namespace lcont couldnt be found(are you missing a using directive or reference) when I build).

Why? I used this approach previously, and it worked.

Here's my master page:

<%@ Register src="Usercontrol/lcont.ascx" tagname="lcont" tagprefix="uc1" %>

 <div class="container" >
<form id="form1" runat="server">

<div class=" row ">
  <div class="col-lg-12">
     <uc3:Header ID="Header1" runat="server" />
  </div>
  </div>

        <div class="col-lg-3" style=" background:rgb(245,245,245); margin-right:1%; min-height:500px; " >
          <uc1:lcont ID="lcont1" runat="server" />
        </div>

Here's its code behind:

public lcont getlcont()
{
    return lcont1;
}

Actually what I want is to access the user control's ID in the ASPX page. The user control is of course included in the master page. Besides this, what else do I need to get around this problem?

If you have implemented lcont in the master page, you surely can make the public function of the type lcont .

The way you are doing it is absolutely correct - exactly how it should be done.

So as far as I know, I think the problem is in the user control's class name. The lcont you have used in the public function should be the class name of the lcont user control; but sometimes the class name is not the user control's file name. I mean if your file name is lcont.ascx , you assume that the class name is lcont - in which case your code will work; but sometimes the class name is foldername_usercontrolfilename - in which case your code won't work.

So what should be done?

  1. Check the first line of the lcont.ascx page. There is a property called Inherit . Check its value, which should be lcont not foldername_lcont .
  2. Check the class declaration in the lcont.ascx.cs file. It is probably public partial class foldername_lcont : System.Web.UI.UserControl . Change it to public partial class lcont : System.Web.UI.UserControl .

Now your master page will recognize lcont .

For more on this, see https://unschoolingcode.wordpress.com/2014/10/16/accessing-usercontrols-id-in-the-aspx-page/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM