I'm having a master page in my website and having some control on master page. I want to handle the master page controls event on content page.
我有一个主页在我的网站和一些控制主页。我想处理内容页上的主页面控件事件。
how can i do that ...?
我该怎么做…?
even i have used property like this for a dropdown control
甚至我也用过这样的属性作为下拉控件
public DropDownList propertymastercontrol
{
get
{
return cmbVendor; //drop down id
}
}
and given the reference of master page on content page like this...
在这样的内容页面上,给出了主页的参考……
<%@ MasterType VirtualPath ="~/MasterPage.master" %>
still after not able to get the any event of control on content page_load event..
仍然无法在content page_load事件上获得任何控制事件。
protected void Page_Load(object sender, EventArgs e)
{
Master.
}
what can be the problem..?
有什么问题?
4 个解决方案
#1
2
I'm not sure about C#, but in VB.NET I've done it with these four steps:
我不确定c#,但是用VB。NET我已经完成了以下四个步骤:
- Add events to the MasterPage code behind, raise these events as required
- 将事件添加到背后的母版代码中,根据需要提升这些事件
- Add this to the content aspx page:
<%@ MasterType VirtualPath="~/master.Master" %>
- 将此添加到内容aspx页面:<%@ MasterType VirtualPath="~/master "。大师”% >
- Add event handlers to trap the event on the content page codebehind (this is where things get very different between C# and VB.NET)
- 添加事件处理程序来捕获content page codebehind上的事件(这是c#和VB.NET之间非常不同的地方)
- Add your required code to the events in the codebehind
- 将所需的代码添加到代码后面的事件中
UPDATE:
更新:
From your sample code, I can see you're not raising an event in the MasterPage (which you can respond to the content page). I was going to give an example, but it would be almost identical to this post, Pass MasterPage ImageButton event to content Page, which another SO user already has linked to in the answers here too.
从您的示例代码中,我可以看到您并没有在母版中引发事件(您可以对内容页面作出响应)。我将给出一个例子,但它与这篇文章几乎是一样的,将母版ImageButton事件传递到内容页面,这是另一个用户已经链接到这里的答案。
#2
0
I am assuming the page_Load event is in the code behind section. You have to declare the master page file there also to be able to pull the contents of the master page.
我假设page_Load事件位于代码的后面部分。您必须声明主页面文件,也可以拉出母版页的内容。
Put this in the on preInit :
把这个放到preInit中:
this.MasterPageFile = "~/masterPage.master"; //change this to where your master page resides.
#3
0
Look at this question I had, very similar:
看看我的问题,非常相似:
Key points, it works just like regular .ASPX page and UserControls. In fact, the MasterPage becomes a UserControl to the content page.
关键点,它的工作方式就像普通的。aspx页面和用户控件一样。事实上,主页变成了内容页面的用户控件。
Pass MasterPage ImageButton event to content Page
将母版ImageButton事件传递到内容页面
#4
0
The quick and dirty way is to find the control in the masterpage and wire it up. Your Page_Load will look something like this:
快速而肮脏的方法是在母版中找到控件并将其连接起来。您的Page_Load将如下所示:
protected void Page_Load(object sender, EventArgs e)
{
DropDownList cmbVendor = (DropDownList)Master.FindControl("cmbVendor");
cmbVendor.TextChanged += new EventHandler(cmbVendor_TextChanged);
}
The cleaner way is to have the MasterPage handle the DropDown event and raise it's own event. The accepted answer for Pass MasterPage ImageButton event to content Page explains how to do this.
更简洁的方法是让母版处理下拉事件并提升它自己的事件。将MasterPage ImageButton事件传递给内容页面的公认答案将解释如何做到这一点。
#1
2
I'm not sure about C#, but in VB.NET I've done it with these four steps:
我不确定c#,但是用VB。NET我已经完成了以下四个步骤:
- Add events to the MasterPage code behind, raise these events as required
- 将事件添加到背后的母版代码中,根据需要提升这些事件
- Add this to the content aspx page:
<%@ MasterType VirtualPath="~/master.Master" %>
- 将此添加到内容aspx页面:<%@ MasterType VirtualPath="~/master "。大师”% >
- Add event handlers to trap the event on the content page codebehind (this is where things get very different between C# and VB.NET)
- 添加事件处理程序来捕获content page codebehind上的事件(这是c#和VB.NET之间非常不同的地方)
- Add your required code to the events in the codebehind
- 将所需的代码添加到代码后面的事件中
UPDATE:
更新:
From your sample code, I can see you're not raising an event in the MasterPage (which you can respond to the content page). I was going to give an example, but it would be almost identical to this post, Pass MasterPage ImageButton event to content Page, which another SO user already has linked to in the answers here too.
从您的示例代码中,我可以看到您并没有在母版中引发事件(您可以对内容页面作出响应)。我将给出一个例子,但它与这篇文章几乎是一样的,将母版ImageButton事件传递到内容页面,这是另一个用户已经链接到这里的答案。
#2
0
I am assuming the page_Load event is in the code behind section. You have to declare the master page file there also to be able to pull the contents of the master page.
我假设page_Load事件位于代码的后面部分。您必须声明主页面文件,也可以拉出母版页的内容。
Put this in the on preInit :
把这个放到preInit中:
this.MasterPageFile = "~/masterPage.master"; //change this to where your master page resides.
#3
0
Look at this question I had, very similar:
看看我的问题,非常相似:
Key points, it works just like regular .ASPX page and UserControls. In fact, the MasterPage becomes a UserControl to the content page.
关键点,它的工作方式就像普通的。aspx页面和用户控件一样。事实上,主页变成了内容页面的用户控件。
Pass MasterPage ImageButton event to content Page
将母版ImageButton事件传递到内容页面
#4
0
The quick and dirty way is to find the control in the masterpage and wire it up. Your Page_Load will look something like this:
快速而肮脏的方法是在母版中找到控件并将其连接起来。您的Page_Load将如下所示:
protected void Page_Load(object sender, EventArgs e)
{
DropDownList cmbVendor = (DropDownList)Master.FindControl("cmbVendor");
cmbVendor.TextChanged += new EventHandler(cmbVendor_TextChanged);
}
The cleaner way is to have the MasterPage handle the DropDown event and raise it's own event. The accepted answer for Pass MasterPage ImageButton event to content Page explains how to do this.
更简洁的方法是让母版处理下拉事件并提升它自己的事件。将MasterPage ImageButton事件传递给内容页面的公认答案将解释如何做到这一点。