2011-05-27 177 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