ASPNET web表单禁用按钮提交 [英] aspnet webforms disable button on submit

查看:156
本文介绍了ASPNET web表单禁用按钮提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Web表单一个小问题。我试图禁用提交按钮提交,以prevent双重职位。
问题是,在codebehind的onclick方法,如果提交按钮被回发期间禁止是没有得到调用。回传仍然存在,但按键的onclick方法不会被调用。
有没有办法来解决这个问题?

下面的问题的一小演示:

 <%@页面语言=C#AutoEventWireup =真codeBehind =default.aspx.cs继承=WebApplication2._default%GT;<!DOCTYPE HTML>< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
    <标题>< /标题>
    <脚本SRC =脚本/ jQuery的-2.0.3.min.js>< / SCRIPT>    <脚本>
        功能disableButton(){
            //$('#<%=btn.ClientID%>').attr('disabled','diabled');
        }
    &LT; / SCRIPT&GT;
&LT; /头&GT;
&LT;身体GT;
    &LT;表ID =form1的=服务器&GT;
    &LT; D​​IV&GT;
        &LT; ASP:文字=服务器ID =ltrDate/&GT;
        &LT; ASP:按钮=服务器ID =BTN的OnClientClick =disableButton();文本=打我!的OnClick =Unnamed_Click/&GT;
    &LT; / DIV&GT;
    &LT; /表及GT;
&LT; /身体GT;
&LT; / HTML&GT;

codebehind:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Threading;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;命名空间WebApplication2
{
    公共部分类_default:System.Web.UI.Page
    {
        保护无效的Page_Load(对象发件人,EventArgs的发送)
        {        }        保护无效Unnamed_Click(对象发件人,EventArgs的发送)
        {
            Thread.sleep代码(1000);
            ltrDate.Text = DateTime.Now.ToString();
        }
    }
}

如果这个code运行时,它工作正常,但如果//从JavaScript方法去除,按钮的onclick方法不再被调用。 (回传仍然存在罚款)

/马丁

更新:
这似乎是一个老问题。


  

我从来没有得到这个工作,因为当按钮被禁用的客户端,它的信息不再回发到服务器,因此它的服务器端的Click事件不会触发。我怀疑这就是你看到的行为。
  如果你设法使这项工作,我很想知道怎么办。
  伊恩·奥尔森
   周三,2004年6月9日



解决方案

好吧。我找到了答案:)
诀窍是使用Page.GetPostBackEventReference(BTN)。该方法将插入一个呼叫到dopostback,右边的事件将被解雇。
最简单的解决方法就是插入禁用该按钮的JavaScript行之后该行。然后,它会工作,但如果你有客户端验证,也不会工作。 (或者,它会工作,但按钮将被禁用提交如果用户忘记输入所需的信息)。

下面是从我原来的问题编辑使用客户端验证演示,并提交按钮的回发的disabeling:

 &LT;%@页面语言=C#AutoEventWireup =真codeBehind =default.aspx.cs继承=WebApplication2._default%GT;&LT;!DOCTYPE HTML&GT;&LT; HTML的xmlns =htt​​p://www.w3.org/1999/xhtml&GT;
&LT;头=服务器&GT;
    &LT;标题&GT;&LT; /标题&GT;
    &LT;脚本SRC =脚本/ jQuery的-2.0.3.min.js&GT;&LT; / SCRIPT&GT;    &LT;脚本&GT;
        $(函数(){
            如果(typeof运算ValidatorUpdateDisplay!='不确定'){
                VAR originalValidatorUpdateDisplay = ValidatorUpdateDisplay;                ValidatorUpdateDisplay =功能(VAL){
                    originalValidatorUpdateDisplay(VAL);                    //绑定();
                    如果(val.isvalid){
                        $('#&LT;%= btn.ClientID%GT;')。ATTR(禁用,已禁用);
                        &所述;%= Page.GetPostBackEventReference(BTN)%GT;
                    }
                }
            }
        });
    &LT; / SCRIPT&GT;
&LT; /头&GT;
&LT;身体GT;
    &LT;表ID =form1的=服务器&GT;
        &LT; ASP:的ScriptManager =服务器/&GT;
    &LT; D​​IV&GT;
        &LT; ASP:文本框=服务器ID =txtFirstname/&GT;
        &LT; ASP:的RequiredFieldValidator =服务器的ControlToValidate =txtFirstname显示=动态EnableClientScript =真的ErrorMessage =请输入您的名字/&GT;
        &LT; BR /&GT;
        &LT; ASP:文字=服务器ID =ltrDate/&GT;
        &LT; BR /&GT;
        &LT; ASP:按钮=服务器ID =BTN文本=打我!的OnClick =Unnamed_Click/&GT;
    &LT; / DIV&GT;
    &LT; /表及GT;
&LT; /身体GT;
&LT; / HTML&GT;

codebehind:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Threading;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;命名空间WebApplication2
{
    公共部分类_default:System.Web.UI.Page
    {
        保护无效的Page_Load(对象发件人,EventArgs的发送)
        {        }        保护无效Unnamed_Click(对象发件人,EventArgs的发送)
        {
            Thread.sleep代码(1000);
            ltrDate.Text = DateTime.Now.ToString();
        }
    }
}

I have a small problem in Webforms. I'm trying to disable the submit button on submit, to prevent double posts. The problem is, that the onclick method in codebehind is not getting called if the submit button is disabled during postback. Postback still occurs, but the buttons onclick method doesn't get called. Is there a way to get around this?

Here a small demo of the problem:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="WebApplication2._default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-2.0.3.min.js"></script>

    <script>
        function disableButton() {
            //$('#<%=btn.ClientID%>').attr('disabled', 'diabled');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Literal runat="server" ID="ltrDate" />
        <asp:Button runat="server" ID="btn" OnClientClick="disableButton();" Text="Hit me!" OnClick="Unnamed_Click" />
    </div>
    </form>
</body>
</html>

codebehind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

        }

        protected void Unnamed_Click(object sender, EventArgs e)
        {
            Thread.Sleep(1000);
            ltrDate.Text = DateTime.Now.ToString();
        }
    }
}

If this code is run, it works fine, but if the // is removed from the javascript method, the buttons onclick method is not being called anymore. (postback still occurs fine)

/Martin

Update: It seems to be an old issue..

I never got this to work, because when the button is disabled client-side, its information is no longer posted back to the server, so its server-side click event does not fire. I suspect that's the behavior you're seeing. If you do manage to make this work, I'd love to know how. Ian Olsen Wednesday, June 09, 2004

解决方案

Ok.. I found the answer :) The trick is to use Page.GetPostBackEventReference(btn). The method will insert a call to dopostback, and the right events will be fired. The easy solution is to just insert that line after the javascript line that disables the button. Then it'll work, but if you have client side validation, that wont work. (Or, it'll work, but the submit button will be disabled if the user forgot to enter the required information).

Here's the demo from my original question edited to use client validation, and with the disabeling of the submit button on postback:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="WebApplication2._default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-2.0.3.min.js"></script>

    <script>
        $(function () {
            if (typeof ValidatorUpdateDisplay != 'undefined') {
                var originalValidatorUpdateDisplay = ValidatorUpdateDisplay;

                ValidatorUpdateDisplay = function (val) {
                    originalValidatorUpdateDisplay(val);

                    //Bind();
                    if (val.isvalid) {
                        $('#<%=btn.ClientID%>').attr('disabled', 'disabled');
                        <%=Page.GetPostBackEventReference(btn)%>
                    }
                }
            }
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server" />
    <div>
        <asp:TextBox runat="server" ID="txtFirstname" />
        <asp:RequiredFieldValidator runat="server" ControlToValidate="txtFirstname" Display="Dynamic" EnableClientScript="true" ErrorMessage="Please enter your firstname" />
        <br />
        <asp:Literal runat="server" ID="ltrDate" />
        <br />
        <asp:Button runat="server" ID="btn" Text="Hit me!" OnClick="Unnamed_Click" />
    </div>
    </form>
</body>
</html>

Codebehind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

        }

        protected void Unnamed_Click(object sender, EventArgs e)
        {
            Thread.Sleep(1000);
            ltrDate.Text = DateTime.Now.ToString();
        }
    }
}

这篇关于ASPNET web表单禁用按钮提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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