开发者

Reference a MasterPage property

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜