views:

342

answers:

6

I am using asp.net webforms; I want to submit a form and keep the button disabled until it gets saved to my database, exactly like Stackoverflow. Any suggestion?

protected void Lb_Save_Click(object sender, EventArgs e)
{
   //disable my button
      //Do a DB insert 
   //enable my button
}

EDIT:

I'll do a client-side validation first so I don't want my button to be disabled there but I want to disable it once the onclick function starts. Hope you all get my point.

Also i would like to do the same for asp:linkbutton but i don't know how firefox behaves for it. As the linkbutton will be rendered as anchor how can i disable it?

Guys you didn't get my question.

How to disable asp:button during postback? (not in jQuery/Javascript)

+3  A: 

Short answer: Javascript.

Longer answer:

$('#formid').submit(function () {
     $('#buttonid').hide();
 $('#disabledbuttonimageid').show();
});
Richard
@Richard will this keep my asp:button disabled untill my pstback finishes..
MuraliVijay CSK
@Murali - for ASP.NET, you also have to take into account the name/id mangling that the framework does. What this means is that you'll have to use a different syntax in your jQuery selector. The syntax shown here would match the control with an id of "buttonid". However, the framework would change this at runtime to "some_big_long_string_buttonid" so your selector would be something like ":button[id$='buttonid']" to find the control whose id *ends* in "buttonid".
GalacticCowboy
+8  A: 

It disables it on submit(), it never enables it again because you're redirected. However, if you wanted to do some sort of validation you would just do

$("#yourForm").submit(function(e) {
    $("#yourButton").attr('disabled','disabled');
    if (whateverValidation) {
        e.preventDefault();
        $("#yourButton").removeAttr('disabled');
    } 
});
Robert
@Robert look at my edit
MuraliVijay CSK
Why wouldn't you want the button disabled immediately to prevent multiple submissions?
Robert
@Robert ya exactly
MuraliVijay CSK
Well, I assumed that upon successful validation you'd actually submit the post, which would redirect, so there was no need to enable the button if the validation passed. The above example will disable the button as soon as you hit submit, if `whateverValidation` is true, it prevents the submission and enables the button.
Robert
@Robert will this work for asp.net webforms which has only one form with id `aspnetForm`
Pandiya Chendur
@Pandiya Chendur it should work with any form id, just replace `#yourForm` with `#aspnetForm` and `#yourButton` with the submit button ID, or another identifier to get the button.
Robert
@Robert how to make this a reusable function to all my submit buttons?
Pandiya Chendur
@Pandiya Chendur: if you have a question that isn't related to this question, post your own! You'll find you'll get a speedier response.
Alastair Pitts
@Alaistair i am looking for the same answer for this question then why post a duplicate?
Pandiya Chendur
the `#yourButton` is the button that gets disabled. Just replace that selector with the selector that matches your button, same thing for `#yourForm`. http://api.jquery.com/category/selectors/
Robert
+1  A: 

Ok, heres some code that can give you an idea on what to do. You will need class DISABLED which will set the appropriate CSS styles of the button or whatever so it appears disabled...

CLIENT SIDE JAVA SCRIPT

function doRequest(MY_DATA_THAT_I_WANT_TO_SEND)
{
    $.ajax({
       type: "GET",
       url: "saving_page.aspx?data="+MY_DATA_THAT_I_WANT_TO_SEND,
       timeout: 10000,
       beforeSend: function()
       {    
           $("#button").toggleClass("DISSABLED");
       },
       complete: function(XMLHttpRequest, textStatus)
       {
           //Or you can remove success handler and let complete hander enables
           //the button, so it gets enabled if there was an error or there was success
           //$("#button").toggleClass("DISSABLED");         
       },                             
       success: function(data)
       {                                            

           if(data == "OK") $("#button").toggleClass("DISSABLED");                       
       }

     });        
};

SERVER SIDE CS

protected void Page_Load(object sender, EventArgs e)
{
    string data = Request.Params.Get("data");
    saveDataToDataBase(data);

    //Sleep some more...
    Thread.Sleep(3000);

    //Send ok
    Response.Write("OK");
    Response.End();
}
Cipi
+2  A: 

How? That's a magic.

Now, if you don't want javascript:

  1. On postback, disable the button.
  2. Do no real work, but set Session variable to indicate what work is needed.
  3. Set HTML meta tag to force immediate refresh.
  4. Return - browser will immediate refresh the page.
  5. When you get request now, you check for Session var, and do the work.

There might be other ways, but frankly they all are gonig to be weird, why don't you want JavaScript?

queen3
@queen how will my button be disabled untill i submit my record to db if i use javascript...
MuraliVijay CSK
That's already answered by other people.
queen3
+2  A: 

The only way to do this with web forms (which is not mvc) is to hide the original button and replace it with a picture of the disabled button or another button which is disabled. If you actually disable the button .net's JavaScript will not do the submit.

(Yes, I know you can "hack" the .net JavaScript, but really, is that any better?)

The JavaScript to do this is simple once you know what you need to do -- if you want to see some code I should be able to dig it up -- just ask in the comments.

NB: MVC should be as simple as the code in the other answers which just disable the button before the ajax call. SO is using MVC so they don't have the problems you are experiencing.

Hogan
A: 

Please try following code.

protected void Button1_Click(object sender, EventArgs e)
{

    //check you conditions

    Button1.Enabled = false;

    //process your form submit

    Button1.Enabled = true;

}
Nagarajan
@Nagarajan have you tried this? It doesn't work
Pandiya Chendur
Yes, I have tried it and it works fine for me. If you are having any problem with this, please provide me your code snippet.
Nagarajan