从母版的方法调用的内容页面方法 [英] Calling Content Page Method from MasterPage Method

查看:165
本文介绍了从母版的方法调用的内容页面方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/887178/content-page-class-method-calling-from-master-page-class\">content网页类的方法从母版页类调用


我需要从母版页事件访问内容页方法。我怎样才能做到这一点?

 内容页:
公共部分类Call_Center_Main:System.Web.UI.Page
{
    的Page_Load(对象发件人,EventArgs的发送)
    {
    }    公共无效MenuClick(字符串ClkMenu)
    {
     //有些code
    }
}母版:
公共部分类母版:System.Web.UI.MasterPage
{
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
    }    保护无效Menu1_MenuItemClick(对象发件人,MenuEventArgs E)
    {
      //我怎么能叫MenuClick方法从内容页从这里?
    }
}


解决方案

这答案摘自<一个href=\"http://www.asp.net/web-forms/tutorials/master-pages/interacting-with-the-content-page-from-the-master-page-cs\">Interacting从母版页

内容页

您可以使用委托做到这一点。

例如,你有一个母版按钮,你要调用从母版页的内容页面的方法。这里是母版页code。

母版页:

 公共部分类母版:System.Web.UI.MasterPage
{
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
    }    保护无效的button1_Click(对象发件人,EventArgs的发送)
    {
        如果(contentCallEvent!= NULL)
            contentCallEvent(这一点,EventArgs.Empty);
    }
    公共事件的EventHandler contentCallEvent;
}

内容页:

 公共部分类Content_1:System.Web.UI.Page
{
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {    }
    私人无效Master_ButtonClick(对象发件人,EventArgs的发送)
    {
        //此方法将被调用。
    }
    保护无效Page_ preINIT(对象发件人,EventArgs的发送)
    {
        //创建母版页的contentCallEvent事件的事件处理程序
        Master.contentCallEvent + =新的EventHandler(Master_ButtonClick);
    }
}

也可以增加以下行指定您在母版路径VirtualPath

 &LT;%@ VirtualPath的MasterType =〜/ MasterPage.master%GT;
//这是强类型引用

Possible Duplicate:
content page class method calling from master page class

I need to access Content Page Method from Master page Event. How can I do this?

Content Page:
public partial class Call_Center_Main : System.Web.UI.Page
{
    Page_Load(object sender, EventArgs e)
    {
    }

    public void MenuClick(string ClkMenu)
    { 
     // Some Code
    }
}

MasterPage:
public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
      //How Can I call MenuClick method from Content Page from Here  ???
    }
}

解决方案

This answer is taken from Interacting with the Content Page from the Master Page

You can do this using Delegates.

For Example, you have a button in MasterPage and you want to call a Method in Content Page from Master Page. Here is the Code in Master Page.

Master Page:

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (contentCallEvent != null)
            contentCallEvent(this, EventArgs.Empty);
    }
    public event EventHandler contentCallEvent;
}

Content Page:

public partial class Content_1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private void Master_ButtonClick(object sender, EventArgs e)
    {
        // This Method will be Called.
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        // Create an event handler for the master page's contentCallEvent event
        Master.contentCallEvent += new EventHandler(Master_ButtonClick);
    }
}

And Also add the Below Line Specifying you MasterPage Path in VirtualPath

<%@ MasterType VirtualPath="~/MasterPage.master" %> 
// This is Strongly Typed Reference

这篇关于从母版的方法调用的内容页面方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆