Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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中从代码隐藏调用Javascript函数#_Javascript_C#_Asp.net - Fatal编程技术网

在C中从代码隐藏调用Javascript函数#

在C中从代码隐藏调用Javascript函数#,javascript,c#,asp.net,Javascript,C#,Asp.net,我在外部JS文件中有一个javascript函数。我想从后面的角落给它打电话。我该怎么做? 我从ScriptManager.RegisterStartupScript上读到了一些我可以做的事情,但有人能解释一下怎么做吗 JS文件: Function1() { alert("came"); //Some More logic } 更新 从 //Tried this But NOT WORKING protected void Page_PreRen

我在外部JS文件中有一个javascript函数。我想从后面的角落给它打电话。我该怎么做? 我从
ScriptManager.RegisterStartupScript
上读到了一些我可以做的事情,但有人能解释一下怎么做吗

JS文件:

    Function1()
    {
      alert("came");
     //Some More logic
    }
更新

//Tried this But NOT WORKING
 protected void Page_PreRender(object sender, EventArgs e)
 {
      ScriptManager.RegisterStartupScript(this, GetType(), "Function1", "Function1();", true);
     Page.ClientScript.RegisterStartupScript(GetType(), "Function1", "Function1()", true);

 }
我经常用这个

string script = string.Format("alert('{0}');",alert);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
基本上,它只是将该行插入到页面中,并在页面加载后立即运行该行

ClientScript.RegisterStartupScript(GetType(),"CallMyFunction","Function1();",true);

另一种方法是

ScriptManager.RegisterStartupScript(第页,typeof(第页),“somekey”,script,true)


这也适用于部分回发。

要严格回答您的问题,您需要这样的答案:

//Following statement is used to call pre-defined javascript function
protected void btnServerSide_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "myFunction", "myFunction();", true);            
}
是一个提供更多细节和示例的网站

还有一个包含多个示例的精彩演练

是上面列出的演练中我最喜欢的,它非常全面

您可以尝试一下

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "CallMyFunction", "myFunction()", true);

示例代码,其中将包含javascript文件,然后从代码后面调用方法

MyJSFile.js

function Test() {
    alert("hi");
}
WebForm1.aspx.cs-

 using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace CodeprojectTest
{
    public partial class WebForm1 : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var js = new HtmlGenericControl("script");
            js.Attributes["type"] = "text/javascript";
            js.Attributes["src"] = "JScript1.js";
            Page.Header.Controls.Add(js);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "Test();", true);
        }
    }
}

将alert定义为您希望在alertMy Function1()中显示的变量,该变量包含一些大的逻辑,我也希望重用这些逻辑。因此,我需要保留在EXt JS文件中。Function1()是否在代码中?从js-->c#进行通信有点棘手。如果是这种情况,那么使用jsp中的ajax函数是最容易的。您是否试图将整个函数传递到页面?这不起作用我只是试图调用函数,而不是传递它。可能的重复可能是因为您试图在处理javascript之前调用它,请注意我的函数是如何通过单击按钮触发的,而您的函数在预渲染中function@hellotemp11试试这个.GetType()而不是这个,GetType()@hellotemp11还可以尝试将一个警报而不是function1()放在一个位置,并检查它是否工作“脚本”和“myScript”应该相同吗?为什么在这里使用这两个变量?
 using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace CodeprojectTest
{
    public partial class WebForm1 : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var js = new HtmlGenericControl("script");
            js.Attributes["type"] = "text/javascript";
            js.Attributes["src"] = "JScript1.js";
            Page.Header.Controls.Add(js);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "Test();", true);
        }
    }
}