0

我在 li.Attributes.Add("class", "current") 上收到空异常错误

母版页:

<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

谢谢。

4

1 回答 1

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
于 2013-11-13T20:07:08.760 回答