Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following:

<button id="btnSend"  runat="server" alt="" CausesValidation="false"  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>


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.

Modified:

All I want to do is disable the button so it the user does not click twice.
C#
private void InitializeComponent()
{
    this.btnSend.ServerClick += new System.EventHandler(this.btnSend_Click);
}
Posted
Updated 8-Feb-11 9:53am
v7

I am not sure there's even a Disabled property. Try setting Enabled to false.

BTW if you mean the HTML attribute, you probably need to set that in client side script.

Here's an example of disabling a button from the client-side:

XML
<input type="button" onclick="this.disabled=1">
 
Share this answer
 
v3
Comments
#realJSOP 8-Feb-11 13:55pm    
There isn't, and you're right - it's Enabled. Not only that, but it's a boolean property, not an enumerated one.
Nish Nishant 8-Feb-11 13:57pm    
It's kinda crazy that people post pseudo-code here instead of their actual code. It prevents us from trying out their actual code to see what's wrong.
Chris Meech 8-Feb-11 14:21pm    
If answering other people's questions were easy, it would have been called Cricket. :)
Nish Nishant 8-Feb-11 14:26pm    
Wow, what's that about?
Chris Meech 8-Feb-11 15:00pm    
Sorry, Nish. I was just taking a dig, but it wasn't too clear.
There's a weird character in the markup:

önserverclick
 
Share this answer
 
v3
Comments
Nish Nishant 8-Feb-11 14:12pm    
I saw that too but I assumed it was due to a 2-level copy/paste (VS to Word to browser).
Please try it this way:

C#
protected void btnSend_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        btnSend.Attributes["disabled"] = "disabled";
        System.Threading.Thread.Sleep(2000);
    }
}


And as already observed the ö should be an o.

Cheers!
 
Share this answer
 
I don't know what you want to do by putting the thread to sleep.
If you wanted to disable the button for 2000 millisecond, it is an incorrect approach.
If you want just wait 2000 millisecond and then disable the button, it works.
Here is the code:

XML
<button id="btnSend" runat="server" alt="" causesvalidation="false">
            <span><b class="va">Send Message</b></span>
        </button>



C#
protected void Page_Load(object sender, EventArgs e)
       {
           this.btnSend.ServerClick += new EventHandler(btnSend_ServerClick);
       }
       void btnSend_ServerClick(object sender, EventArgs e)
       {
           btnSend.Disabled = true;
           System.Threading.Thread.Sleep(2000);
         
       }
 
Share this answer
 
v2
If you want to make sure the user doesn't click twice in rapid succession you would have to handle this on the client side in javascript. Sleeping on the server side is not going to help you here. Try it along the lines of this here sample:

XML
<button id="btnSend"  runat="server" alt="" CausesValidation="false"  onserverclick="btnSend_Click" onclick="lockButton(this)">

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

</button>


and on your page you need some javascript:

JavaScript
function lockButton(button)
{
    button.disabled = true;
}


Since you also have a server side function that will be executed you'll not even have to worry about resetting the value to false. When the page has made the roundtrip back from the server the button will be enabled again unless you chose to do other wise.

Cheers!
 
Share this answer
 
<pre lang="cs">protected void btnSend_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        btnSend.Disabled = true;
        System.Threading.Thread.Sleep(2000);
    }
}


this code can not working in code behind(ASP.NET). because ASP.NET have not properties Disabled.
that It use visible and enabled.
You can use code below
C#
protected void btnSend_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        btnSend.Visible = False; //button is hidded
        System.Threading.Thread.Sleep(2000);
    }
}

or
C#
protected void btnSend_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        btnSend.Enabled = False; //button is hidded
        System.Threading.Thread.Sleep(2000);
    }
}


good lucky!
 
Share this answer
 
v2
Try removing CausesValidation="false" or Set the CausesValidation="true"
 
Share this answer
 
v2
Comments
macupryk 8-Feb-11 20:39pm    
best way is to put this in a panel.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900