2011-05-27 117 views
2

我有这样的代码要添加的类别:一类添加到一个锚链接

(Master.FindControl("ControlName")).Attributes.Add("class", "menu-selected"); 

但是,这并不为链接的工作:

<a href="Default.aspx" id="mnuHome" runat="server">Home</a> 

但引发错误:

'System.Web.UI.Control' does not contain a definition for 'Attributes' and no extension method 'Attributes' accepting a first argument of type 'System.Web.UI.Control' could be found (are you missing a using directive or an assembly reference?)

我可以把我所有的链接都变成Hyperlink服务器控件,但是这会变得非常混乱,如果可能,我宁愿保留它。是吗?

回答

4

你需要转换你的控制:

((HtmlAnchor)(Master.FindControl("ControlName"))).Attributes.Add("class", "menu-selected"); 

UPDATE
这是HtmlAnchor,而不是以前的状态HtmlGenericControl