2

i have save button in around 150 pages. when ever the user clicks on save button i should disable the button after the click. so that user does not keep clicking on save button again.

   protected void Button1_Click(object sender, EventArgs e)
    {
      // right now once the  user  clicks the save button
       Button1.Enabled = "false"

    }

right now i am doing like this. is there any better solution u can provide me to improve codeing here would be great.

thank you

happysmile
  • 7,537
  • 36
  • 105
  • 181

5 Answers5

3

I think the best option to accomplish this is using javascript. If you are using jQuery (which I can even start to recommend enough) then you can put the javascript in your masterpage. You just need to find a way to create a selector for your save buttons. Like this:

$(document).ready(function(){
        $('.saveButton').bind("click", function(e) {
                $(this).attr("disabled", "true");
            return true; //causes the client side script to run.
        });
    });

In this example, I assumed that all the save buttons would have the css class ".saveButton", but you can find your own way to select the button.

Cheers!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gus
  • 596
  • 4
  • 7
  • @Gus: if you post code like your jQuery snippet, make sure to highlight the lines and then click on the "code" button (010 101) on the editor toolbar or press Ctrl-K on your keyboard to neatly format it as code. Then and only then will it be syntax highlighted and all. – marc_s Oct 17 '09 at 21:19
  • hi Gus, thanks for the replay if i use the above code in master page. intally during the click of the button it should get ex try { public void save function() { // here all save button should be disabled } catch(exception e) { throw e; } finally { //here my save button should be Enabled // finally my save button should get enable here } } – happysmile Oct 26 '09 at 09:12
2

You can also try disabling it via Javascript.

Carles Company
  • 7,118
  • 5
  • 49
  • 75
1

You need to use JavaScript to alter the link once it's clicked to prevent future click handling, before allowing the page to proceed with the postback. A naive attempt would be,

<asp:LinkButton runat="server" id="button" OnClientClick="this.href='#'">
    Click - doesn't quite work
</asp>

This successfully prevents successive clicks from triggering the postback, but it also prevents the first click from triggering the postback. A little more logic is required to make this work correctly. In the codebehind, I grab the actual postback JavaScript snippet and work it into some logic.

protected void Page_PreRender(object sender, EventArgs e)
{
    button1.OnClientClick = string.Format(
        "if(this.getAttribute('disabled')) return false; {0}; this.setAttribute('disabled','disabled'); return false;",
        Page.ClientScript.GetPostBackEventReference(button1, null));
}

In the HTML template:

<asp:LinkButton runat="server" id="button" >
    Click - does not allow multiple postbacks
</asp>

You can spruce this up with some CSS, and at that point I would advise including jQuery, so your code is more concise (due to its command chaining).

G-Wiz
  • 7,370
  • 1
  • 36
  • 47
0

What you have to do is use some JavaScript and disable it when its clicked on the client side.

That click event fires on a postback.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

Simplest way of attaching javascript to disable the button after user click would be this (in page onLoad event):

myButton.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(btnSave, null) + ";");

But if button is repeated on many pages, you can maybe create nested master page and put save button there, or inherit you page from class that implements this disable functionality. Also, you can "scan" for control you want to disable after click!

Hrvoje Hudo
  • 8,994
  • 5
  • 33
  • 42