如何将类分配给
  • 之一从子页面通过 C# 在母版页中?
  • 发布于 2024-09-11 22:41:20 字数 888 浏览 3 评论 0原文

    我有一个带有一堆菜单的母版页,例如:

    <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 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(2

    相守太难 2024-09-18 22:41:20

    这是一个已知问题。我建议在这种情况下不要使用代码块(最好的做法是使用尽可能少的代码块)。

    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).

    假情假意假温柔 2024-09-18 22:41:20

    这个链接有正确的答案,事实上,有人可以删除这个问题,因为它是一个重复的问题,我没有意识到这一点。

    我终于通过以下方式让它工作:

    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

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文