使用按钮在 aspx.cs 上调用 JavaScript 函数(在 aspx 中) [英] Call JavaScript function (in aspx) on aspx.cs using a button

查看:42
本文介绍了使用按钮在 aspx.cs 上调用 JavaScript 函数(在 aspx 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 aspx:

 <div><script type="text/javascript">函数 NewPage() {document.location.href = "http://www.nextservice.pt/"}<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"/></表单>

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

 公共部分类 SITE_TESTER : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}protected void Button2_Click(对象发送者,EventArgs e){字符串代码 = TextBox1.Text.ToString();if (!verifyCode(code))//比较表中的用户{Label1.Text = "不存在";//对于无效代码}别的{Label1.Text = "存在";//获取成功代码/*我不会在这里调用我的 JavaScript 函数!!!!*/}}}

解决方案

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

protected void button_Click(object sender , EventArgs e){string jsMethodName== "NewPage()";ScriptManager.RegisterClientScriptBlock(this, typeof(string), "uniqueKey", jsMethodName, true);//或者//ScriptManager.RegisterStartupScript(this, GetType(), "NewPage()", false);}

您可以使用 ScriptManager.RegisterStartupScriptScriptManager.RegisterClientScriptBlock

因此两者之间的区别解释如下:

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

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

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

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

B.Page.RegisterClientScriptBlock() 通常用于脚本封装在函数中.(因此有了块"这个词)Page.RegisterStartupScript() 可用于任何脚本,即使它是不在函数中.

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

D.Page.RegisterClientScriptBlock() 应该用于执行以下操作的脚本不需要创建表单元素.Page.RegisterStartupScript() 应该用于需要已创建的表单元素并使用对它们的引用.

请注意,所有 4 个差异本质上都与每个差异相关其他(它们建立在上一个之上).差异放在一行有时可能过于微妙.

您可以从此处此处

I have this 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

   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!!!!

            */
        }
    }
}

解决方案

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

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.RegisterStartupScript or ScriptManager.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)

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 block of script before Line 2. Page.RegisterStartupScript() will insert the script after Line 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.

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.

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.

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.

you can know more about these from here and here

这篇关于使用按钮在 aspx.cs 上调用 JavaScript 函数(在 aspx 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆