我在li.Attributes.Add(“class”,“current”)上得到一个null异常错误
母版页:
<ul>
<li id="li01"><a href="page1.aspx" title="Page 1">Page 1</a</li>
<li id="li02"><a href="page2.aspx" title="Page 2">Page 2</a></li>
<ul>
页面代码背后:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim li As System.Web.UI.HtmlControls.HtmlGenericControl = DirectCast(Me.Page.Master.FindControl("li02"), System.Web.UI.HtmlControls.HtmlGenericControl)
li.Attributes.Add("class", "current")
End Sub
感谢。
答案 0 :(得分:0)
删除me.Page
的参考请尝试以下代码:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim li As System.Web.UI.HtmlControls.HtmlGenericControl = DirectCast(Master.FindControl("li02"), System.Web.UI.HtmlControls.HtmlGenericControl)
li.Attributes.Add("class", "current")
End Sub