使用按钮在 aspx.cs 上调用 JavaScript 函数(在 aspx 中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14578911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:16:56  来源:igfitidea点击:

Call JavaScript function (in aspx) on aspx.cs using a button

javascriptasp.net.netwebforms

提问by kaub0st3r

I have this aspx:

我有这个aspx:

 <body>
    <div>
    <script type="text/javascript">
        function NewPage() {
            document.location.href = "http://www.nextservice.pt/"
        }
        </script>
         <form id="form1" runat="server">
 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

                    <asp:Button ID="Btn2" runat="server" Text="OK" onclick="Button2_Click" />

            CODE1: <asp:Label ID="Label1" runat="server" Text="Label" ForeColor="#CC0000" />
        </form>
        </div>
</body>

and I'm working with web forms, and I wont call this button on aspx.cs

我正在处理网络表单,我不会在 aspx.cs 上调用这个按钮

   public partial class SITE_TESTER : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button2_Click (object sender, EventArgs e)
    {
        string code = TextBox1.Text.ToString();

        if (!verifyCode(code))  // comparing users from table 
        {
            Label1.Text = "Not Exists";  //for invalid code
        }
        else
        {
            Label1.Text = "Exist";  //for sucsseful code
            /*

            I Wont call my JavaScript Function here!!!!

            */
        }
    }
}

回答by Manish Mishra

you can call a javascript method from server side in asp.net by following ways:

您可以通过以下方式在 asp.net 中从服务器端调用 javascript 方法:

protected void button_Click(object sender , EventArgs e)
{
        string jsMethodName= = "NewPage()";
        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "uniqueKey", jsMethodName, true);

      //or
      //ScriptManager.RegisterStartupScript(this, GetType(), "NewPage()", false); 
}

you can use either ScriptManager.RegisterStartupScriptor ScriptManager.RegisterClientScriptBlock

你可以使用ScriptManager.RegisterStartupScriptScriptManager.RegisterClientScriptBlock

so difference between the two is explained below:

所以两者之间的区别解释如下:

Let's say we have a .aspx page with the following form tag : (Line nos. are for reference)

假设我们有一个带有以下表单标签的 .aspx 页面:(行号仅供参考)

1. <form id="Form1" runat="server">
2. ..
3. ..
4. ..
5. </form>

Now let's look at key differences for each method :

现在让我们看看每种方法的主要区别:

A. Page.RegisterClientScriptBlock()will insert the blockof script before Line 2. Page.RegisterStartupScript()will insert the script after Line 4.

A. Page.RegisterClientScriptBlock()将在第 2 行之前插入脚本Page.RegisterStartupScript()将在第 4 行之后插入脚本。

B. Page.RegisterClientScriptBlock()should usually be used for scripts encapsulated in functions. (hence the word "block") Page.RegisterStartupScript()can be used for any script, even if it's not in a function.

B. Page.RegisterClientScriptBlock()通常用于封装在函数中的脚本。(因此“块”一词) Page.RegisterStartupScript()可用于任何脚本,即使它不在函数中。

C. Page.RegisterClientScriptBlock()should be used for functions that don't need to run on Page load. Page.RegisterStartupScript()should be used for scripts that must run on Page Load.

C. Page.RegisterClientScriptBlock()应该用于不需要在页面加载时运行的功能。 Page.RegisterStartupScript()应该用于必须在页面加载时运行的脚本。

D. Page.RegisterClientScriptBlock()should be used for a script that does not require the form elements to have been created. Page.RegisterStartupScript()should be used for scripts that require the form elements to have been created and uses references to them.

D. Page.RegisterClientScriptBlock()应用于不需要创建表单元素的脚本。 Page.RegisterStartupScript()应该用于需要创建表单元素并使用对它们的引用的脚本。

Notice that all the 4 differences are essentially related to each other (they build upon the prev. one). The difference put in one line can sometimes be too subtle.

请注意,所有 4 个差异本质上都是相互关联的(它们建立在上一个差异的基础上)。一行中的差异有时可能过于微妙。

you can know more about these from hereand here

你可以从这里这里了解更多关于这些的信息

回答by Viktor S.

You can add a script which will be executed when page is loaded to browser:

您可以添加将在页面加载到浏览器时执行的脚本:

Page.RegisterStartupScript("unique_key", "<script type=\"text/javascript\">NewPage()</script>"); // but this is deprecated function

or like this:

或者像这样:

ClientScript.RegisterClientScriptBlock(this.GetType(), "unique_key", "NewPage()", true);            

But if you simply want to do a redirect (as I can see from your NewPage function), you can do:

但是如果你只是想做一个重定向(正如我从你的 NewPage 函数中看到的那样),你可以这样做:

Response.Redirect("http://www.example.com");