开发者

Reference a MasterPage property

开发者 https://www.devze.com 2023-02-28 02:59 出处:网络
I have a master page with the following code: public partial class MasterPage : System.Web.UI.MasterPage

I have a master page with the following code:

public partial class MasterPage : System.Web.UI.MasterPage
{
    public SqlConnection cnx;
    protected void Page_Load(object sender, EventArg开发者_开发技巧s e)
    {
 
    }
}

How do I reference the public SqlConnection cnx property from an aspx.cs file that uses this master page?


In your master page:

    public SqlConnection CnxInMasterPage
    {
        get { return this.cnx; }
    }

In Content page (first add using so you can reference 'MasterPage' type)

var cnx = ((MasterPage)Master).CnxInMasterPage;


You have a couple of options:

  1. cast the Master property to your MasterPage type and proceed from there.
  2. Include <%@ MasterType virtualpath="~/path/to/master.master" %> in your aspx file which will strongly type the Master property.


You should declare an interface IMyMasterPage and put the property there. Allow your master page to implement it.

Then you can do this on your page.

var myMasterPage = this.MasterPage as IMyMasterPage
0

精彩评论

暂无评论...
验证码 换一张
取 消