【问题标题】:How to write jQuery inside of C#如何在 C# 中编写 jQuery
【发布时间】:2015-05-15 11:24:40
【问题描述】:

我需要一个 Javascript 或 jquery 在我的 C# 代码隐藏中 如果我将它放在 div 中,我会像它应该的那样工作。但现在我想把脚本放在 if else 里面

查看id为chart2的部分

    <div style="position: absolute; top: 0; left: 0; height: 100%; width: 100%; z-index:0; ">
        <asp:Literal ID="ltrRenderChart" runat="server"></asp:Literal>
    </div>
    <div  ID="chart2"style="position: absolute; top: 0; left: 50%; height: 100%; width: 100%; z-index:-1">
       <script>$("#chart2").css("z-index",0);</script>
        <asp:Literal ID="ltrRenderChart2" runat="server"></asp:Literal>
    </div>
    <div style="position: absolute; top: 50%; left: 0; height: 100%; width: 100%; z-index:-1;">
        <asp:Literal ID="ltrRenderChart3" runat="server"></asp:Literal>
    </div>
    <div style="position: absolute; top: 50%; left: 50%; height: 100%; width: 100%; z-index:-1;">
        <asp:Literal ID="ltrRenderChart4" runat="server"></asp:Literal>
    </div> 
</div>

但是如果我把它写在 if-else 里面它不会影响任何东西。

                  else if (n == 2 && chart.Width == "50%")
                    {

                        literal = ltrRenderChart2;   
                        string script = "<script>$(\"#chart2\").css(\"z-index\",0);</script>";
                        ScriptManager.RegisterClientScriptBlock(ltrRenderChart2, ltrRenderChart2.GetType(), "Script", script,false);
                    }
                    else if (n == 2 && chart.Height == "50%")
                    {
                       literal = ltrRenderChart2;

                         string str = "<script>$(\"#chart2\").css(\"z-index\",0);</script>";
                        ScriptManager.RegisterClientScriptBlock(ltrRenderChart2, ltrRenderChart2.GetType(), "Script", str, false);
                    }

有人知道这个问题吗?

谢谢

【问题讨论】:

  • 你的页面中加载了 jQuery 吗?
  • 这里的chart 是什么?
  • 小心&lt;script&gt; 标签!我记得,Visual Studio 在 ascx 和 aspx 文件中存在一些重大问题,因为它将其解析为服务器端脚本标记!我记得有时我们不得不像这样拆分它:"&lt;scr" + "ipt&gt;"。在我第一次遇到这种行为之前,我简直不敢相信......
  • @Markus

标签: javascript c# jquery z-index scriptmanager


【解决方案1】:

如果你在 C# 中这样写:

string script = "<script>$(\"#chart2\").css(\"z-index\",0);</script>";

然后 C# 会将其视为字符串值。

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script ", script , true);

通过使用 ScriptManager,您可以将 javascript 脚本添加到页面中,如上所示

【讨论】:

  • 我刚刚发现你不允许写
猜你喜欢
  • 2011-01-02
  • 1970-01-01
  • 2011-02-13
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多