FindControl 在页面中查找控件
我的 aspx 页面上有一个月份列表,如下所示:
<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>
在我的代码后面,如果列表项代表本月。
我尝试执行以下操作来连接两个字符串以获得对页面上控件的引用:
CType(Page.FindControl("Month" & Now.Month), HtmlControl).Attributes("class") += " selected"
这似乎不起作用,所以我想知道是否是因为该页面位于母版页中,所以我将代码更改为以下内容:
CType(Page.Master.FindControl("Content").FindControl("Month" & Now.Month), HtmlControl).Attributes("class") += " selected"
“Content”是母版页中 ContentPlaceHolder 的 ID。
这仍然不起作用,所以有人可以告诉我我做错了什么吗?谢谢。
编辑: 我刚刚意识到代码实际上位于 ascx 文件中,而不是我上面放置的 aspx 文件。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试
我
:Try
Me
: