禁止在.NET应用程序中使用jQuery的岗位后退按钮 [英] Disable buttons on post back using jquery in .net app

查看:67
本文介绍了禁止在.NET应用程序中使用jQuery的岗位后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想尽快禁用按钮,因为他们被点击,以prevent多个提交一个asp.net应用程序。我想使用jQuery此作为该网站已经采用宽松也无妨。

我已经试过是:

  $(文件)。就绪(函数(){
    $(#aspnetForm)。递交(函数(){
        $(输入[类型=提交]',$(本))ATTR(禁用,禁用)。
    })
});

以上将禁用按钮,页面提交,但在单击处理asp.net按钮永远不会被调用。简单地删除上述和按钮恢复正常。

有没有更好的办法?或者说,我是什么做错了吗?

更新
好吧,我终于有了一点时间把一个很简单的页面在一起。

 <%@页面语言=C#AutoEventWireup =真codeBehind =SubTest.aspx.cs继承=MyTesting.SubTest%GT;!< D​​OCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
    <标题>< /标题>
    <脚本SRC =脚本/ jQuery的-1.4.1.min.js类型=文/ JavaScript的>< / SCRIPT>
    <脚本类型=文/ JavaScript的>
        $(函数(){
            $(#Form1的)。递交(函数(){
                $(输入[类型=提交]',$(本))ATTR(禁用,禁用)。
            });
        });
    < / SCRIPT>
< /头>
<身体GT;
    <表ID =form1的=服务器>
    < D​​IV>
        < ASP:按钮的ID =Button1的=服务器的onclick =的button1_Click文本=按钮/>
        < ASP:按钮的ID =Button2的=服务器的onclick =Button2_Click文本=按钮2/>
    < / DIV>
    < /表及GT;
< /身体GT;
< / HTML>

在code背后是这样的:

 使用系统;命名空间MyTesting {
    公共部分类分测验:System.Web.UI.Page {
    保护无效的Page_Load(对象发件人,EventArgs的发送){
        如果(的IsPostBack){
            //当任何按钮pressed这将执行
            的Response.Write(回传);
        }
    }
        保护无效的button1_Click(对象发件人,EventArgs的发送){
        //从不执行
            System.Threading.Thread.Sleep(1000);
            的Response.Write(按钮1点击< BR />中);
        } //方法::的button1_Click        保护无效Button2_Click(对象发件人,EventArgs的发送){
        //从不执行
            System.Threading.Thread.Sleep(1000);
            的Response.Write(按钮2点击< BR />中);
        } //方法:: Button2_Click
    }
}

当你点击一个按钮,它显然是禁用的按钮,但无论是按钮的点击中运行。

渲染HTML

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/ XHTML1-transitional.dtd>< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
< HEAD><标题>< /标题>
    <脚本SRC =脚本/ jQuery的-1.4.1.min.js类型=文/ JavaScript的>< / SCRIPT>
    <脚本类型=文/ JavaScript的>
        $(函数(){
            $(#Form1的)。递交(函数(){
                $(输入[类型=提交]',$(本))ATTR(禁用,禁用)。
            });
        });
    < / SCRIPT>
< /头>
<身体GT;
    <表格名称=Form1的方法=邮报行动=SubTest.aspxID =form1的>
< D​​IV>
<输入类型=隐藏的名字=__ VIEWSTATEID =__ VIEWSTATEVALUE =/ wEPDwUKMTcxODU4OTc0MWRkParC5rVFUblFs8AkhNMEtFAWlU4 =/>
< / DIV>< D​​IV>    <输入类型=隐藏的名字=__ EVENTVALIDATIONID =__ EVENTVALIDATIONVALUE =/ wEWAwKB57WhCAKM54rGBgK7q7GGCC6LlWKFoij9FIBVuI0HOVju / FTY/>
< / DIV>
    < D​​IV>
        <输入类型=提交名称=Button1的值=按钮ID =Button1的/>
        <输入类型=提交名称=Button2的值=按钮2ID =Button2的/>
    < / DIV>
    < /表及GT;
< /身体GT;
< / HTML>


解决方案

我只是想增加一个额外的分辨率。我们决定只完全删除按钮,一旦它被点击,并与一些文本替换它。

要做到这一点,我们所做的:

  $(函数(){
    $(。DisableButton)。点击(函数(){
        $(本).hide();
        $(本)。之后('< P>请等待检索信息,这可能需要60秒-1; / P>');    });
});

请注意,这个隐藏,然后单击按钮注入按钮code后,一些HTML。隐藏它允许.NET来继续运行后在onclick处理回,同时消除它作为在屏幕上可点击的事情。

I have a asp.net app that I want to disable the buttons as soon as they are clicked in order to prevent multiple submissions. I'd like to use jquery for this as the site already liberally uses it anyway.

What I've tried is:

$(document).ready(function () {
    $("#aspnetForm").submit(function () {
        $('input[type=submit]', $(this)).attr("disabled", "disabled");
    })
});

The above will disable the button, and the page submits, but the asp.net button on click handler is never called. Simply removing the above and the buttons work as normal.

Is there a better way? Or, rather, what am I doing wrong?

UPDATE Okay, I finally had a little time to put a very simple page together.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SubTest.aspx.cs" Inherits="MyTesting.SubTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#form1").submit(function () {
                $('input[type=submit]', $(this)).attr("disabled", "disabled");
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button 2" />
    </div>
    </form>
</body>
</html>

The code behind looks like:

using System;

namespace MyTesting {
    public partial class SubTest : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
        if (IsPostBack) {
            // this will execute when any button is pressed
            Response.Write("postback");
        }
    }
        protected void Button1_Click(object sender, EventArgs e) {
        // never executes
            System.Threading.Thread.Sleep(1000);
            Response.Write("Button 1 clicked<br />");
        } // method::Button1_Click

        protected void Button2_Click(object sender, EventArgs e) {
        // never executes
            System.Threading.Thread.Sleep(1000);
            Response.Write("Button 2 clicked<br />");
        } // method::Button2_Click
    }
}

When you click on a button it obviously disables the buttons, but NEITHER of the button clicks are run.

Rendered HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#form1").submit(function () {
                $('input[type=submit]', $(this)).attr("disabled", "disabled");
            });
        });
    </script>
</head>
<body>
    <form name="form1" method="post" action="SubTest.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTcxODU4OTc0MWRkParC5rVFUblFs8AkhNMEtFAWlU4=" />
</div>

<div>

    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwKB57WhCAKM54rGBgK7q7GGCC6LlWKFoij9FIBVuI0HOVju/fTy" />
</div>
    <div>
        <input type="submit" name="Button1" value="Button" id="Button1" />
        <input type="submit" name="Button2" value="Button 2" id="Button2" />
    </div>
    </form>
</body>
</html>

解决方案

I just wanted to add an additional resolution. We decided to just completely remove the button once it was clicked and replace it with some text.

To do this we did:

$(function () {
    $(".DisableButton").click(function () {
        $(this).hide();
        $(this).after('<p>Please Wait.  Retrieving information.  This may take up to 60 seconds.</p>');

    });
});

Note that this hides the button then injects some html after the buttons code. Hiding it allows .Net to go ahead and run the onclick handler during post back while removing it as a clickable thing on the screen.

这篇关于禁止在.NET应用程序中使用jQuery的岗位后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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