ASP.Net双击问题 [英] ASP.Net double-click problem

查看:96
本文介绍了ASP.Net双击问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有我的一个ASP.net页面一个小问题。如果用户双击一个提交按钮将会写入到数据库中的两倍(即开展的onclick的方法在ImageButton的两倍)

having a slight problem with an ASP.net page of mine. If a user were to double click on a "submit" button it will write to the database twice (i.e. carry out the 'onclick' method on the imagebutton twice)

我怎样才能使它所以,如果一个用户点击的ImageButton,刚刚的ImageButton被禁用?

How can I make it so that if a user clicks on the imagebutton, just the imagebutton is disabled?

我试过:

<asp:ImageButton 
     runat="server" 
     ID="VerifyStepContinue" 
     ImageUrl=image src 
     ToolTip="Go" 
     TabIndex="98" 
     CausesValidation="true" 
     OnClick="methodName" 
     OnClientClick="this.disabled = true;" />

但是,这OnClientClick属性完全被提交停止页面!任何帮助吗?

But this OnClientClick property completely stops the page from being submitted! Any help?

对不起,是的,我确实有验证控件......因此,过甜的问题。

Sorry, yes, I do have Validation controls... hence the icky problem.

现在这方面的工作还,到了这一点:

Working on this still, up to this point now:

ASP code:

 <asp:TextBox ID="hidToken" runat="server" Visible="False" Enabled="False"></asp:TextBox>
 ...
 <asp:ImageButton runat="server" ID="InputStepContinue" Name="InputStepContinue" ImageUrl="imagesrc" ToolTip="Go" TabIndex="98" CausesValidation="true" OnClick="SubmitMethod" OnClientClick="document.getElementById('InputStepContinue').style.visibility='hidden';" />

C#code:

C# code:

         private Random
    random = new Random();


    protected void Page_Load(object sender, EventArgs e)
    {
        //Use a Token to make sure it has only been clicked once.
        if (Page.IsPostBack)
        {
            if (double.Parse(hidToken.Text) == ((double)Session["NextToken"]))
            {
                InputMethod();
            }
            else
            {
                // double click
            }
        }

        double next = random.Next();

        hidToken.Text = next + "";
        Session["NextToken"] = next;

其实......这几乎工程。双击的问题是pretty很多固定的(耶!)图像仍没有隐藏,但。

Actually... this nearly works. The double click problem is pretty much fixed (yay!) The image still isn't hidden though.

推荐答案

一般的方法是双重的。

The general approach is twofold.

Serverside集团


  1. 在页面负载,生成令牌(使用 System.Random ),将其保存在会话,并把它写入一个隐藏的表单字段

  2. 在提交,检查隐藏的表单字段等于会话变量(的的再设置)

  3. 请工作

  1. On load of the page, generate a token (using System.Random), save it in the session, and write it to a hidden form field
  2. On submit, check that the hidden form field equals the session variable (before setting it again)
  3. Do work

客户方

类似的你有什么,但很可能只是隐藏按钮,像'提交'一些文本替换它。

Similar to what you have, but probably just hide the button, and replace it with some text like 'submitting'.

重要的是要注意的,客户端是,用户可以通过点击逃离取消后,所以你应该考虑在这里做什么(取决于走多远他们令牌将不能使用,所以你需要从被带上按钮回到禁用/隐藏)。

The important thing to note, client side, is that the user may cancel the post by hitting 'escape', so you should consider what to do here (depending on how far along they are the token won't be used, so you'll need to bring the button back from being disabled/hidden).

完整的例子如下:

C#(包括code看到它在行动):

C# (includes code to see it in action):

<html>
<head runat="server">
    <title>double-click test</title>
    <script language="c#" runat="server">
    private Random
        random = new Random();

    private static int
        TEST = 0;

    public void Page_Load (object sender, EventArgs ea)
    {
        SetToken();
    }

    private void btnTest_Click (object sender, EventArgs ea)
    {
        if( IsTokenValid() ){
            DoWork();
        } else {
            // double click
            ltlResult.Text = "double click!";
        }
    }

    private bool IsTokenValid ()
    {
        bool result = double.Parse(hidToken.Value) == ((double) Session["NextToken"]);
        SetToken();
        return result;
    }

    private void SetToken ()
    {
        double next = random.Next();

        hidToken.Value       = next + "";
        Session["NextToken"] = next;
    }


    private void DoWork ()
    {
        TEST++;
        ltlResult.Text = "DoWork(): " + TEST + ".";
    }
    </script>
</head>
<body>
    <script language="javascript">
        var last = null;
        function f (obj)
        {
            obj.src = "http://www.gravatar.com/avatar/4659883ec420f39723c3df6ed99971b9?s=32&d=identicon&r=PG";
            // Note: Disabling it here produced strange results. More investigation required.
            last = obj;
            setTimeout("reset()", 1 * 1000);
            return true;
        }

        function reset ()
        {
            last.src = "http://www.gravatar.com/avatar/495ce8981a5127a9fd24bd72e7e3664a?s=32&d=identicon&r=PG";
            last.disabled = "false";
        }
    </script>
    <form id="form1" runat="server">
        <asp:HiddenField runat="server" ID="hidToken" />
        <asp:ImageButton runat="server" ID="btnTest"
            OnClientClick="return f(this);"
            ImageUrl="http://www.gravatar.com/avatar/495ce8981a5127a9fd24bd72e7e3664a?s=32&d=identicon&r=PG" OnClick="btnTest_Click" />
        <pre>Result: <asp:Literal runat="server" ID="ltlResult" /></pre>
    </form>
</body>
</html>

这篇关于ASP.Net双击问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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