在嵌套母版页中更改或添加列表项类

时间:2013-11-13 20:01:01

标签: asp.net vb.net

我在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

感谢。

1 个答案:

答案 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