How should I access public methods of master page from a child page?
UserMaster.master.vb
Public Sub UpdateCart()
End Sub
Default.aspx.cs
How can I access UpdateCart()
from the Default.aspx.cs page?
From you Content page you can use this to achieve the requirement and make sure it marked as a public not protected:
VB
TryCast(Me.Master, MyMasterPage).UpdateCart()
C#
(this.Master as MyMasterPage).UpdateCart();
Do it like this:
SiteMaster master = new SiteMaster();
//now call the master page method
master.test()
Example
//master page code behind
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
//test method
public void test()
{
}
}
//content page code behind
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SiteMaster master = new SiteMaster();
master.test();
}
}
或者将SiteMaster
方法SiteMaster
static
并直接调用它:
SiteMaster.MyStaticMethod()
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.