How do I raise an event in a content page that its master page can then respond to?
如何在内容页面中引发其主页可以响应的事件?
For example, I have a content page which is using a master page. The master page contains the website navigation bar. Depending on the page I'm on, I'd like to send an event to the master page to change the CSS class of the current page menu item in the navigation bar.
例如,我有一个使用母版页的内容页面。母版页包含网站导航栏。根据我所在的页面,我想将一个事件发送到母版页以更改导航栏中当前页面菜单项的CSS类。
I'm using Visual Studio 2008 and ASP.NET 3.5 and C#.
我正在使用Visual Studio 2008和ASP.NET 3.5和C#。
Thanks!
5 个解决方案
#1
Edit, from what your asking you don't really need an event, you just want to call a method on the master. off the top of my head, in the master:
编辑,从你的要求你真的不需要一个事件,你只想在主人上调用一个方法。在我的头顶,在主人:
public void ChangeCss(string className)
{
someObject.CssClass = className;
}
then in your page:
然后在你的页面中:
(this.Master as MyMasterType).ChangeCss("myclass");
#2
If the event happens on all content pages use Kirtan's BasePage solution. If a base page isn't appropriate, then within each page where the event happens add this when the page loads.
如果事件发生在所有内容页面上,请使用Kirtan的BasePage解决方案。如果基页不合适,那么在事件发生的每个页面中,在页面加载时添加此页面。
thisPage.Event += (Page.Master as YourMasterPageClass).YourCustomEventHandler
#3
Create a base page class. Create the respective delegate & event for the same. Then in the master page, do this -
创建基页类。为此创建相应的委托和事件。然后在母版页中,执行此操作 -
(Page as BasePage).Event += attach event handler here in the master page.
The page class must be like this -
页面类必须是这样的 -
public class MyPage : BasePage { }
The BasePage class must be like this -
BasePage类必须是这样的 -
public class BasePage : System.Web.UI.Page { } //This will go in the App_Code folder.
#4
An event doesn't seem like the best way to indicate this, given you need your masterpage to understand your pages, you could well have a standard property on the pages that indicates their 'key' in the navigation.
事件似乎不是表明这一点的最佳方式,因为您需要您的母版页来理解您的网页,您可能会在页面上有一个标准属性,用于指示导航中的“键”。
In your masterpage code:
在您的母版页代码中:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
var navigatable = this.Page as INavigatable;
if (navigatable != null)
this.Navigation.ActiveKey = navigatable .NavigationKey;
}
Navigatable interface:
public interface INavigatable
{
string NavigationKey { get; }
}
Page instance:
public class AboutPage : Page, INavigatable
{
public string NavigationKey
{
get { return "About"; }
}
}
#5
You can raise the event using a technique called as EventBubbling.
您可以使用名为EventBubbling的技术引发事件。
#1
Edit, from what your asking you don't really need an event, you just want to call a method on the master. off the top of my head, in the master:
编辑,从你的要求你真的不需要一个事件,你只想在主人上调用一个方法。在我的头顶,在主人:
public void ChangeCss(string className)
{
someObject.CssClass = className;
}
then in your page:
然后在你的页面中:
(this.Master as MyMasterType).ChangeCss("myclass");
#2
If the event happens on all content pages use Kirtan's BasePage solution. If a base page isn't appropriate, then within each page where the event happens add this when the page loads.
如果事件发生在所有内容页面上,请使用Kirtan的BasePage解决方案。如果基页不合适,那么在事件发生的每个页面中,在页面加载时添加此页面。
thisPage.Event += (Page.Master as YourMasterPageClass).YourCustomEventHandler
#3
Create a base page class. Create the respective delegate & event for the same. Then in the master page, do this -
创建基页类。为此创建相应的委托和事件。然后在母版页中,执行此操作 -
(Page as BasePage).Event += attach event handler here in the master page.
The page class must be like this -
页面类必须是这样的 -
public class MyPage : BasePage { }
The BasePage class must be like this -
BasePage类必须是这样的 -
public class BasePage : System.Web.UI.Page { } //This will go in the App_Code folder.
#4
An event doesn't seem like the best way to indicate this, given you need your masterpage to understand your pages, you could well have a standard property on the pages that indicates their 'key' in the navigation.
事件似乎不是表明这一点的最佳方式,因为您需要您的母版页来理解您的网页,您可能会在页面上有一个标准属性,用于指示导航中的“键”。
In your masterpage code:
在您的母版页代码中:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
var navigatable = this.Page as INavigatable;
if (navigatable != null)
this.Navigation.ActiveKey = navigatable .NavigationKey;
}
Navigatable interface:
public interface INavigatable
{
string NavigationKey { get; }
}
Page instance:
public class AboutPage : Page, INavigatable
{
public string NavigationKey
{
get { return "About"; }
}
}
#5
You can raise the event using a technique called as EventBubbling.
您可以使用名为EventBubbling的技术引发事件。