如何将类分配给
我有一个带有一堆菜单的母版页,例如:
<ul id="ulMenu" runat="server">
<li id="A">
<li id="B">
<li id="C">
..and so on ..
</ul>
我在母版页中有一个函数,我从子页面调用该函数,例如:
Master.setCurrentMenu("A");
在母版页的 SetCurrentMenu() 中,因为我有我的 < UL>设置为在服务器上运行,我动态地更改它,例如:(
StringBuilder myMenu = new StringBuilder(ulMenu.InnerHtml);
//replace and add class for current page
myMenu .Replace("<li id=\"" + cp + "\"", "<li class='selected' id=\""+ cp +"\"");
ulMenu.InnerHtml = myMenu.ToString();
其中 cp 是当前上下文中的“A”)
这样我就可以更改innerHTML,理论上它应该可以正常工作。但是,在我的< UL>我之间有一些服务器标签,例如 <% ... %>所以现在asp.net非常高兴地告诉我:
无法修改Controls集合,因为该控件包含代码块(即<% ... %>)。
在这种情况下我能做的最好的事情是什么?
非常感谢!
PS:编辑此内容是因为我的< ul>被视为 html 而不是显示出来.. 嗯
I have a master page with a bunch of menu like:
<ul id="ulMenu" runat="server">
<li id="A">
<li id="B">
<li id="C">
..and so on ..
</ul>
and i have a function in master page which i call from child pages, like:
Master.setCurrentMenu("A");
and in the SetCurrentMenu() of the Masterpage, since I have my < UL > set to run at server, i am changing it dynamically, like:
StringBuilder myMenu = new StringBuilder(ulMenu.InnerHtml);
//replace and add class for current page
myMenu .Replace("<li id=\"" + cp + "\"", "<li class='selected' id=\""+ cp +"\"");
ulMenu.InnerHtml = myMenu.ToString();
(where cp is the "A" in the current context)
This way I am changing the innerHTML and theoretically its supposed to work just fine. but, in my < UL > i have a few server tags in between like <% ... %> so now the asp.net tells me very cheerfully that :
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
What is the best thing I can do in this scenario?
Many thanks!
ps: edited this coz my < ul > were being taken as an html instead of showing up.. hmm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个已知问题。我建议在这种情况下不要使用代码块(最好的做法是使用尽可能少的代码块)。
It's a known issue. I would suggest not using code blocks in this case (it's considered a best practice use as little code blocks as possible).
这个链接有正确的答案,事实上,有人可以删除这个问题,因为它是一个重复的问题,我没有意识到这一点。
我终于通过以下方式让它工作:
C# - 如何更改 HTML元素属性
This link has the correct answer, infact, somebody can delete this question since its a repeated one, I dint realize it.
I finally got this to work via:
C# - How to change HTML elements attributes