[RESOLVED] <button disable="disable" does not work if I want to disable behind

I have the following: I would like to disable protected void btnSend_Click(object sender, EventArgs e) { if (Page.IsValid) { btnSend.Disabled = true; System.Threading.Thread.Sleep(2000); } this does not wor

I have the following:
<button id="btnSend" runat="server" alt="" CausesValidation="false" ├Ânserverclick="btnSend_Click">
<img id="imgSend" alt="" runat="server" src="~/Images/Buttons/send_ico.gif"
align="top" />


 Send Message</button>


I would like to disable


protected void btnSend_Click(object sender, EventArgs e)
{


if (Page.IsValid)
{
btnSend.Disabled = true;
System.Threading.Thread.Sleep(2000);
}
this does not work.


k.

Can you explain what you are tyring to do? Disabling button when page is valid to stop duplicate postbacks. If so, you can do this...

Markup:

<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="WebForm5.aspx.cs"
    Inherits="WebApplication1.WebForm5" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="txt" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ErrorMessage="Required" ControlToValidate="txt" ForeColor="Red"
        Display="Dynamic" SetFocusOnError="true" runat="server" />
    <asp:Button ID="buttonSave" runat="server" CausesValidation="true" Text="Do Something"
        OnClick="buttonSave_Click" />
    </form>
</body>
</html>

CodeBehind:

protected void Page_Load(object sender, EventArgs e)
{
    buttonSave.Attributes.Add("onclick""Page_ClientValidate();if(Page_IsValid){this.disabled=true;" +
                                Page.ClientScript.GetPostBackEventReference(buttonSave, "Test") + ";}");

}
protected void buttonSave_Click(object sender, EventArgs e)
{
    Thread.Sleep(2000);
}

Try that way and see if it works. Button will be disabled when page is valid.

sometimes the user can click the button twice.

I want to disable this.

 

buttonSave.Attributes.Add("onclick""Page_ClientValidate();if(Page_IsValid){this.disabled=true;" +
                                Page.ClientScript.GetPostBackEventReference(buttonSave, "Test") + ";}");

this is only good for the asp.net button asp:button tag.

 

<button id="btnSend" runat="server" alt="" CausesValidation="true" onserverclick="btnSend_Click">
                        <img id="imgSend" alt="" runat="server" src="~/Images/Buttons/send_ico.gif"
                            align="top" />
                        <span> <b class="va">Send Message</b></span></button>


this is a html button.

it does not work like a asp:button.

Hi,

Accroding to your code, it seems that the button will be disabled after 2 seconds. Is it what you want to implement?

If I'm not misundertood you, you can try to comment the code "System.Threading.Thread.Sleep(2000)".

Or use the client side javascript:

btnSend.Attributes["onclick"] = "this.disabled = true;";

Here System.Threading.Thread.Sleep(2000) is used to do a delay time manually in order to let us see how this disable property affect the button before the button sumbit.

Above poster give you a good example, whatever the button is asp:button or HtmlButton. It should always work.

I modify it for HtmlButton:

protected void Page_Load(object sender, EventArgs e)
        {
           btnSend.Attributes["onclick"] = "this.disabled = true;";
         }






        protected void btnSend_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(2000);
        }


 <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <button id="btnSend" runat="server" alt="send" causesvalidation="false"  onserverclick="btnSend_Click">
                        <img id="img1" alt="" runat="server" src="~/Images/20100920105445544.jpg" align="top" />
                        Send Message</button>
                </ContentTemplate>
            </asp:UpdatePanel>


The button will disabled 2 seconds.

Hope this can help you.

thoughts on "[RESOLVED] <button disable="disable" does not work if I want to disable behind"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>