开发者

FindControl to find a control in the page

开发者 https://www.devze.com 2023-03-20 05:43 出处:网络
I have a list of months on my aspx page as follows: <ul> <li><a id=\"Month1\" runat=\"server\" class=\"Month1\" href=\"#\">Jan</a></li>

I have a list of months on my aspx page as follows:

<ul>
    <li><a id="Month1" runat="server" class="Month1" href="#">Jan</a></li>
    <li><a id="Month2" runat="server" class="Month2" href="#">开发者_运维问答;Feb</a></li>
    <li><a id="Month3" runat="server" class="Month3" href="#">Mar</a></li>
    <li><a id="Month4" runat="server" class="Month4" href="#">Apr</a></li>
    <li><a id="Month5" runat="server" class="Month5" href="#">May</a></li>
    <li><a id="Month6" runat="server" class="Month6" href="#">Jun</a></li>
    <li><a id="Month7" runat="server" class="Month7" href="#">Jul</a></li>
    <li><a id="Month8" runat="server" class="Month8" href="#">Aug</a></li>
    <li><a id="Month9" runat="server" class="Month9" href="#">Sep</a></li>
    <li><a id="Month10" runat="server" class="Month10" href="#">Oct</a></li>
    <li><a id="Month11" runat="server" class="Month11" href="#">Nov</a></li>
    <li><a id="Month12" runat="server" class="Month12" href="#">Dec</a></li>
</ul>

In my code behind, I'm trying to set a class of 'selected' on the <a> tag if the list item represents the current month.

I have tried doing the following to concatenate two strings to obtain a reference to the control on the page:

CType(Page.FindControl("Month" & Now.Month), HtmlControl).Attributes("class") += " selected"

That doesn't seem to work and so I wondered if it was because the page is within a master page, so I altered the code to the following:

CType(Page.Master.FindControl("Content").FindControl("Month" & Now.Month), HtmlControl).Attributes("class") += " selected"

"Content" is the ID of the ContentPlaceHolder in the master page.

This still doesn't work so can someone please tell me what I'm doing wrong? Thanks.

EDIT: I have just realised the code is actually in an ascx file, not an aspx file as I have put above.


Try Me:

CType(Me.FindControl("Month" & Now.Month), HtmlControl).Attributes("class") += " selected"
0

精彩评论

暂无评论...
验证码 换一张
取 消