繁体   English   中英

C#asp.net WebForm添加JS并从后面的代码运行

[英]C# asp.net WebForm add JS and run it from code behind

我有这个WebForm Html

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GetLink.aspx.cs" Inherits="GetLink" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="hidden" runat="server" id="hdnVal" value="55"/>

    </div>
    </form>
</body>
</html>

我想在此代码中添加一个JavaScript函数并使用以下代码运行它:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!ClientScript.IsStartupScriptRegistered("key1"))
        {
            ClientScript.RegisterStartupScript(GetType(), "key1", @"<script type=""text/javascript"">function callMyJSFunction() { document.getElementById(""hdnVal"").value='5'; }</script>");
        }

        ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>callMyJSFunction();</script>");

        string resutOfExecuteJavaScript = hdnVal.Value;
    }

当我运行它时, hdnVal的值保持55值,并且没有变化。 知道是什么问题吗?

注册callMyJSFunction的JavaScript函数时,在Page_Load事件中的代码应调用ClientScript.RegisterClientScriptBlock ,而在代码中,您将此函数注册为启动脚本。 这是代码中的唯一错误。

因此,如果将服务器端代码更改为以下代码,则它将按照您的期望工作。

protected void Page_Load(object sender, EventArgs e)
{
    if (!ClientScript.IsClientScriptBlockRegistered("key1"))
    {
        //register your javascript function
        ClientScript.RegisterClientScriptBlock(GetType(), "key1", @"<script type=""text/javascript"">function callMyJSFunction() { document.getElementById(""hdnVal"").value='5'; }</script>");
    }

    ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>callMyJSFunction();</script>");

    string resutOfExecuteJavaScript = hdnVal.Value;
}

第一个问题是您要在Clientscript中创建函数,而您可以简单地将函数放入javascript中,然后执行调用部分。第二个问题是,您的函数正在调用该hiddenfield以查看其在文档中不可用的时间只是意味着停止放置您的代码会在页面加载时使用按钮点击事件。第三个问题是,您在许多不必要的地方使用了多个反逗号。

这对我有用

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script>
      $('document').ready()
      {
          function callMyJSFunction()
          {
              debugger;
              document.getElementById('hdnVal').value = '5';
 alert(document.getElementById('hdnVal').value);
          }
          // - including fonts, images, etc.
      }
   </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
     <input type="hidden" runat="server" id="hdnVal" value="55"/>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    </div>
    </form>
</body>
</html>

并在cs页面上

  protected void Button1_Click(object sender, EventArgs e)
    {


        ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>callMyJSFunction()</script>",false);

        string resutOfExecuteJavaScript = hdnVal.Value;
    }

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM