【问题标题】:Call JavaScript function from C#从 C# 调用 JavaScript 函数
【发布时间】:2013-09-21 11:18:43
【问题描述】:

Javascript.js

function functionname1(arg1, arg2){content}

C# 文件

public string functionname(arg)
{
    if (condition)
    {
        functionname1(arg1,arg2); // How do I call the JavaScript function from C#?
    }
}

请参考上面的代码并建议我从 C# 调用 JavaScript 函数的想法。

【问题讨论】:

  • 没有得到你。你想在 C# 代码文件中调用 js 函数吗?还是代码文件函数到JS?

标签: c# javascript asp.net function


【解决方案1】:

如果你想在 C# 中调用 JavaScript 函数,这将帮助你:

public string functionname(arg)
{
    if (condition)
    {
        Page page = HttpContext.Current.CurrentHandler as Page;
        page.ClientScript.RegisterStartupScript(
            typeof(Page),
            "Test",
            "<script type='text/javascript'>functionname1(" + arg1 + ",'" + arg2 + "');</script>");
    }
}

【讨论】:

    【解决方案2】:

    这可能对你有帮助:

    <script type="text/javascript">
        function Showalert() {
            alert('Profile not parsed!!');
            window.parent.parent.parent.location.reload();
        }
        function ImportingDone() {
            alert('Importing done successfull.!');
            window.parent.parent.parent.location.reload();
        }
    </script>
    
    if (SelectedRowCount == 0)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "Showalert();", true);
    }
    else
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "importingdone", "ImportingDone();", true);
    }
    

    【讨论】:

      【解决方案3】:

      .aspx 文件在标题部分

      <head>
          <script type="text/javascript">
              <%=YourScript %>
              function functionname1(arg1,arg2){content}
          </script>
      </head>
      

      .cs 文件

      public string YourScript = "";
      public string functionname(arg)
      {
          if (condition)
          {
              YourScript = "functionname1(arg1,arg2);";
          }
      }
      

      【讨论】:

        【解决方案4】:

        您可以使用我组织的开源库Jering.Javascript.NodeJS 从 c# 调用 javascript 函数:

        string javascriptModule = @"
        module.exports = (callback, x, y) => {  // Module must export a function that takes a callback as its first parameter
            var result = x + y; // Your javascript logic
            callback(null /* If an error occurred, provide an error object or message */, result); // Call the callback when you're done.
        }";
        
        // Invoke javascript
        int result = await StaticNodeJSService.InvokeFromStringAsync<int>(javascriptModule, args: new object[] { 3, 5 });
        
        // result == 8
        Assert.Equal(8, result);
        

        该库也支持直接从 .js 文件调用。假设您有文件 C:/My/Directory/exampleModule.js 包含:

        module.exports = (callback, message) => callback(null, message);
        

        您可以调用导出的函数:

        string result = await StaticNodeJSService.InvokeFromFileAsync<string>("C:/My/Directory/exampleModule.js", args: new[] { "test" });
        
        // result == "test"
        Assert.Equal("test", result);
        

        【讨论】:

          猜你喜欢
          • 2017-03-20
          • 2023-03-28
          • 1970-01-01
          • 1970-01-01
          • 2013-08-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多