从内容页面后面的代码访问ASP母版页上的HTML控件 [英] Access an HTML control on ASP Master Page from the code behind of a Content Page
问题描述
我有一个ASP.NET/C#应用程序,其中的主页"包含我的应用程序的主菜单和一些依赖于此主页的内容页面.
I have an ASP.NET / C# application in which the Master Page contain the main menu of my application and several content pages that depend of this master page.
我要突出显示与显示的当前内容页面相对应的母版页面的菜单链接.
I would like to highlight the menu link of my master page corresponding to the current content page displayed.
为此,我已经有一个专用于此的CSS类(称为选定")
To do that, I already have a CSS class dedicated to this (called "selected")
因此,我试图访问要使用其内容ID从内容页面突出显示的母版页"链接,并执行类似的操作(在内容页面中):
Thus, I was trying to access the Master Page link I want to highlight from the content page by using its ID and do something like that (in the content page) :
HtmlLink currentMenu = (HtmlLink) Master.FindControl("idOfTheLinkToHighlight");
currentMenu.Attributes.Add("class", "selected");
但是我得到了以下异常:
But I get the following exception :
Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlGenericControl' to type 'System.Web.UI.HtmlControls.HtmlLink
有人可以帮我吗? 谢谢
Can anybody help me on this ? Thanks
推荐答案
顺便尝试一下
(HtmlGenericControl)currentMenu = (HtmlGenericControl) Master.FindControl("idOfTheLinkToHighlight");
currentMenu.Attributes.Add("class", "selected");
它应该起作用,因为HtmlGenericControl也具有属性
it should work because HtmlGenericControl has also attributes
这篇关于从内容页面后面的代码访问ASP母版页上的HTML控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!