简体   繁体   English

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

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

I have this WebForm Html : 我有这个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>

And i want to add to this code a JavaScript function and run it with this code: 我想在此代码中添加一个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;
    }

When i run it the value of hdnVal keep the 55 value and not change. 当我运行它时, hdnVal的值保持55值,并且没有变化。 Any idea what is the problem? 知道是什么问题吗?

Your code in Page_Load event should call ClientScript.RegisterClientScriptBlock when registering the JavaScript function of callMyJSFunction , whereas in your code you are registering this function as a startup script. 注册callMyJSFunction的JavaScript函数时,在Page_Load事件中的代码应调用ClientScript.RegisterClientScriptBlock ,而在代码中,您将此函数注册为启动脚本。 This is the only mistake in your code. 这是代码中的唯一错误。

So, if you change your server-side code to as below, then it will work according to your expectations. 因此,如果将服务器端代码更改为以下代码,则它将按照您的期望工作。

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;
}

The first problem is you are creating function in Clientscript while you can simply put the function in javascript and then just do the calling part.Second problem is that the time your function is calling that hiddenfield for view its not available on document simply means stop putting your code on page load and use a button click event instead.Third problem is you are using multiple inverted commas at so many places which aren't required. 第一个问题是您要在Clientscript中创建函数,而您可以简单地将函数放入javascript中,然后执行调用部分。第二个问题是,您的函数正在调用该hiddenfield以查看其在文档中不可用的时间只是意味着停止放置您的代码会在页面加载时使用按钮点击事件。第三个问题是,您在许多不必要的地方使用了多个反逗号。

This worked for me 这对我有用

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

and on cs page 并在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