简体   繁体   English

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

[英]Calling Javascript function from Code Behind in C#

I have a javascript function in the External JS File. 我在外部JS文件中有一个javascript函数。 I want to call it from the co0de behind. 我想从后面的代码中调用它。 How can I do that ? 我怎样才能做到这一点 ? I read somewhere that I can do from ScriptManager.RegisterStartupScript but how, can anyone explain me. 我在ScriptManager.RegisterStartupScript可以读取的内容,但是有人可以向我解释。

JS File: JS文件:

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

Update 更新资料

Calling from 致电

//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);

 }

I've often used this 我经常用这个

string script = string.Format("alert('{0}');",alert);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);

basically, it just inserts that line onto your page and runs it as soon as the page loads 基本上,它只是将该行插入页面并在页面加载后立即运行

you can try 你可以试试

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

OR 要么

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

another approach is 另一种方法是

ScriptManager.RegisterStartupScript(Page, typeof(Page), "somekey", script, true);

this will works during partial postbacks also. 这也将在部分回发期间起作用。

To strictly answer your question you are looking for something like this: 为了严格回答您的问题,您正在寻找这样的东西:

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

Here is a site that provides some more details and examples 是一个提供更多详细信息和示例的网站

MSDN also has a great walkthrough with multiple examples MSDN还具有大量示例的出色演练

This is my favorite of the walkthroughs listed above, it is very comprehensive 是上面列出的演练中我最喜欢的,非常全面

sample code, which will include javascript file and then call method from the code behind. 示例代码,其中将包括javascript文件,然后从后面的代码中调用方法。

MyJSFile.js MyJSFile.js

function Test() {
    alert("hi");
}

WebForm1.aspx.cs - 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);
        }
    }
}

您可以试试这个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM