Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C#代码调用java脚本?_C#_Javascript_Asp.net - Fatal编程技术网

如何从C#代码调用java脚本?

如何从C#代码调用java脚本?,c#,javascript,asp.net,C#,Javascript,Asp.net,我有一个Enable.js文件,它有一个Enable()函数。我想从C#codebehind调用这个Enable函数。我的.js文件和c#文件在同一个应用程序中 我试过这个,但对我没有帮助 Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "Enable(true)", true); 试试这个: ScriptManager.RegisterClientScriptInclude(

我有一个
Enable.js
文件,它有一个
Enable()
函数。我想从C#codebehind调用这个
Enable
函数。我的
.js
文件和c#文件在同一个应用程序中

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

Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "Enable(true)", true);
试试这个:

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


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

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


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

我基于刚才看到的一些VB编写了一个很好的小函数,用于在C#中调用jquery或通用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')”)

我基于刚才看到的一些VB编写了一个很好的小函数,用于在C#中调用jquery或general 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')”)

您在这里犯了一个错误:

Page.ClientScript.RegisterStartupScript(this.GetType(),“单击”,“启用(true)”,true)

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

您应该尝试这种方法,这样可能会有所帮助。
这只是一个理解的示例

string func = "showSuccessMessage('"+name+"');";
//Pass string as funcion in c#
将解释如何使用C#代码隐藏中的参数调用javascript函数

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

您在这里犯了一个错误:

Page.ClientScript.RegisterStartupScript(this.GetType(),“单击”,“启用(true)”,true)

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

您应该尝试这种方法,这样可能会有所帮助。
这只是一个理解的示例

string func = "showSuccessMessage('"+name+"');";
//Pass string as funcion in c#
将解释如何使用C#代码隐藏中的参数调用javascript函数

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "xx",   
"<script>test("+x+","+y+");</script>");
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),“xx”,
“测试(“+x+”、“+y+”);”;
我可以在代码中看到“单击”。因此,我假设您需要单击某个按钮来调用Enable.js文件中的Enable(true)函数

按照以下步骤操作:

  • 请参考下面的
    部分中的Enable.js文件

    
    
  • Enable.js文件如下所示:

    function Enable(var){
        alert(val)
    }
    
  • 要在
    按钮1
    的单击事件上调用
    启用()
    函数:

    protected void Page_Load(object sender, EventArgs e){
        Button1.Attributes.Add("OnClick", "return Enable('true');");
    }
    
  • 如果您需要更多帮助,请告诉我。

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

    按照以下步骤操作:

  • 请参考下面的
    部分中的Enable.js文件

    
    
  • Enable.js文件如下所示:

    function Enable(var){
        alert(val)
    }
    
  • 要在
    按钮1
    的单击事件上调用
    启用()
    函数:

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

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

    会发生什么?是否存在JavaScript错误?响应HTML中是否包含
    已启用(true)
    ?(请记住,只有在收到响应后才会在客户端上发生。)查看浏览器控制台并告诉我们您是否有任何错误。您在enable.js中实际在做什么,并且能够看到任何错误吗?发生了什么?是否存在JavaScript错误?响应HTML中是否包含
    已启用(true)
    ?(请记住,只有在收到响应后,客户端才会发生这种情况。)查看您的浏览器控制台,告诉我们您是否有任何错误。您在enable.js中实际在做什么,并且能够看到任何错误吗?我认为ScriptManager仅在使用诸如Update Panel之类的Ajax控件时使用。这可能没有帮助。我认为ScriptManager仅在使用诸如Update Panel之类的Ajax控件时使用。这可能没有帮助。