重写 ASP.net 中的母版页功能
在我的母版页中,
<div class ="breadcrumbs" runat="server"><%= SitePath()%></div>
SitePath() 行调用一个自定义类,该类根据静态站点地图和动态生成的页面的组合构建导航元素。 它返回自定义面包屑导航元素的 html。
这是我的代码后面的 SitePath() 代码,
public string SitePath()
{
BreadCrumbNav breadNav = new BreadCrumbNav();
breadNav.divClass = ".dv";
breadNav.homeTitle = "ABC Home";
return breadNav.Build();
}
我希望能够从我的动态页面覆盖它,以便我可以添加到路径中。 例如...
public override string SitePath()
{
BreadCrumbNav breadNav = new BreadCrumbNav();
breadNav.divClass = ".dv";
breadNav.homeTitle = "ABC Home";
breadNav.AddPage("Cooking Equipment", "PathyGoodness/Cooking+Equipment.ASPX");
breadNav.AddPage("Toasters", "PathyGoodness/Cooking+Equipment/Toasters.ASPX");
return breadNav.Build();
}
有没有办法将母版页方法纳入范围,以便我可以覆盖它们 - 或者我是否需要以不同的方式解决这个问题? 感觉就像我错过了一些非常明显的东西,但我似乎被困住了。
感谢您的帮助!
In my master page I have the line
<div class ="breadcrumbs" runat="server"><%= SitePath()%></div>
SitePath() involks a custom class that builds navigation elements based on the combination of the static sitemap and dynamically generated pages. It returns the html for a custom breadcrumb navigation element.
Here is the SitePath() code from my code behind
public string SitePath()
{
BreadCrumbNav breadNav = new BreadCrumbNav();
breadNav.divClass = ".dv";
breadNav.homeTitle = "ABC Home";
return breadNav.Build();
}
I'd like to be able to override this from my dynamic pages so I can add to the path. For Example...
public override string SitePath()
{
BreadCrumbNav breadNav = new BreadCrumbNav();
breadNav.divClass = ".dv";
breadNav.homeTitle = "ABC Home";
breadNav.AddPage("Cooking Equipment", "PathyGoodness/Cooking+Equipment.ASPX");
breadNav.AddPage("Toasters", "PathyGoodness/Cooking+Equipment/Toasters.ASPX");
return breadNav.Build();
}
Is there a way to bring the master page methods into scope so I can override them- or do I need to go about this in a different way? It feels like I'm missing something really obvious, but I seem to be stuck.
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于母版页实际上并不是继承的,而是用作模板,因此这不是直接可能的。
您可以尝试其他一些方法:
首先,将其放入 ContentPlaceholder 中,并仅在必要时在各个页面的 ASPX 标记中覆盖它。
更复杂的方法是为所有页面创建一个继承的接口,以保证它们具有 SitePath() 方法。 相反,从Master调用页面上的该方法,并且在那些不“覆盖”行为的页面中,只需调用Master即可。 在其他方法中,将您的具体实现添加到该方法中,瞧! (我想您还可以使用抽象 BasePage 类型的类从该方法调用 Master)
Since the Master page is not actually inherited, but rather used as a template, this is not directly possible.
A few other approaches you could try:
Firstly, put this in a ContentPlaceholder and override it in the ASPX markup of your individual pages only where necessary.
A more complicated approach would be to create an interface for all your pages to inherit from that guarantees they have a SitePath() method. Call that method on the page instead, from the Master, and in those pages that do not "override" the behavior, simply call the Master. In others, add your specific implementation to that method, and voila! (I suppose you could also use an abstract BasePage-type class to call the Master from that method)
一般来说,我反对将页面/上下文特定的方法放入母版页类中。 为什么? 因为,正如 Josh 提到的,Masterpages 只是模板。 您永远不会从母版页继承,并且在没有访问 Page 对象的权限的情况下无法显示母版页。 因此,将此类方法放在 Page 类中更有意义。
Generally im against putting page/context specific methods in the masterpage class. Why? Because, as Josh mentioned, Masterpages is just templates. You'll never inherit from a masterpage and you can't show a masterpage without also having access to a Page object. Therefor it makes more sense to put such methods in the Page class.
一种更简单的方法是直接从子页面引用主控件。
在母版页中,将 sitepath() 函数替换为文本控件,如下所示:
然后在母版页的代码中使用以下代码:
在子页面中,您可以使用 Master.FindControl 方法覆盖它,如下所示:
Easy peasy并且不要乱搞事件处理程序或接口或类似的东西。
A much simpler way is to reference a master control directly from your child page.
In your master page, replace the sitepath() function with a literal control like so:
then in the code for the master page use this:
and in the child page you can override this by using the Master.FindControl method like this:
Easy peasy and no messing around with eventhandlers or interfaces or anything like that.