【问题标题】:How to call java script from C# code?如何从 C# 代码调用 javascript?
【发布时间】:2013-02-22 09:44:03
【问题描述】:

我有一个具有Enable() 函数的Enable.js 文件。我想从 C# 代码隐藏中调用这个 Enable 函数。我的.js 文件和 c# 文件在同一个应用程序中。

我试过这个,但对我没有帮助。

Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "Enable(true)", true);

【问题讨论】:

  • 会发生什么?有 JavaScript 错误吗? Enabled(true) 是否包含在响应 HTML 中? (请记住,这只会发生在客户端收到响应之后。)
  • 查看您的浏览器控制台,如果您遇到任何错误,请告诉我们。
  • 你在 enable.js 中实际做了什么,你能看到任何错误吗..?

标签: c# javascript asp.net


【解决方案1】:

试试这个:

   ScriptManager.RegisterClientScriptInclude(
                this,
                typeof(Page),
                "Enable-js",
                ResolveClientUrl("~/scripts/Enable.js"));


ScriptManager.RegisterStartupScript(
                this, GetType(), Guid.NewGuid().ToString(),
                "Enable(True);", true); 

http://msdn.microsoft.com/pt-br/library/bb337005.aspx

【讨论】:

  • 我认为只有在使用像更新面板这样的 Ajax 控件时才会使用 ScriptManager。这可能没有帮助。
【解决方案2】:

我可以在您的代码中看到“点击”。因此,我假设您需要单击某个按钮来调用 Enable.js 文件中的 Enable(true) 函数

按照以下步骤操作:

  1. <head> 部分中引用您的 Enable.js 文件,如下所示。

    <script src="Scripts/Enable.js" type="text/javascript"></script>
    
  2. Enable.js 文件如下:

    function Enable(var){
        alert(val)
    }
    
  3. Button1的点击事件上调用Enable()函数:

    protected void Page_Load(object sender, EventArgs e){
        Button1.Attributes.Add("OnClick", "return Enable('true');");
    }
    

如果您需要更多帮助,请告诉我。

【讨论】:

    【解决方案3】:

    我基于前段时间看到的一些 VB 编写了一个不错的小函数,用于调用 jquery 或 C# 中的一般 JS。

    public bool runJQueryCode(string message)
    {
        ScriptManager requestSM = ScriptManager.GetCurrent(Page);
    
        if (requestSM != null && requestSM.IsInAsyncPostBack)
        {
            ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), Guid.NewGuid().ToString(), getjQueryCode(message), true);
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), Guid.NewGuid().ToString(), getjQueryCode(message), true);
        }
    
        return true;
    }
    
    private string getjQueryCode(string jsCodetoRun)
    {
        string x = "";
    
        x += "$(document).ready(function() {";
        x += jsCodetoRun;
        x += " });";
    
        return x;
    }
    

    所以你可以直接调用 runJqueryCode("alert('hey')");

    【讨论】:

      【解决方案4】:

      你在这里犯了一个错误:

      Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "Enable(true)", true);

      Enable(true) 不可能是您尝试作为参数传递的文字字符串 true

      你应该试试这个方法,这样可能会有所帮助。Its only a sample for understanding

      string func = "showSuccessMessage('"+name+"');";
      //Pass string as funcion in c#
      

      This Link 将解释使用 C# 代码后面的参数调用 javascript 函数。

      this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "xx",   
      "<script>test("+x+","+y+");</script>");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多