0

Have this code:

<asp:UpdatePanel ID="id">
 <asp:CommandField ButtonType="Image" InsertImageUrl="url/here" ShowInsertImage="true" ValidationGroup="valGr" />
</asp:UpdatePanel>

How to prevent double clicking on this button in C# code.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
Morar Mihai
  • 77
  • 1
  • 2
  • 7
  • Maybe some javascript that eats any click event that takes place within X milliseconds of the first click? – Abe Miessler Feb 27 '13 at 18:41
  • Yes, you are right, i have found some javascript code for this, but i don't know how to find this button, because how i know asp autogenerate html code and it complicated for me to find this button. – Morar Mihai Feb 27 '13 at 18:44
  • Yeah the autogenerated ID's are a pain. If you post the javascript I can show you how to do the element selection – Abe Miessler Feb 27 '13 at 18:56
  • i have made a search through the stack overflow and i found something here: http://stackoverflow.com/questions/2830542/prevent-double-submission-of-forms-in-jquery – Morar Mihai Feb 27 '13 at 19:02
  • Ok, well if you are trying to select by ID then use the following code to get the id of the HTML element: `<%= id.ClientID =>` (this will give you the client id of your update panel. If you want the client id for another element, just substitute that element's id. – Abe Miessler Feb 27 '13 at 19:16
  • well, i will try something on that. thanks. – Morar Mihai Feb 27 '13 at 19:19

1 Answers1

0

You can first create a flag that hold the submit and add it to the form. Then open and close that flag when the UpdatePanel go for update and while is waiting for the return.

For example this code is allow the submit of the form (and of the UpdatePanel) only when the fAlloToSubmit is true:

<script>
    var fAlloToSubmit = true;

    function AllowFormToRun()
    {
      if(!fAlloToSubmit)
          alert("Please wait for the page to fully re-loaded.");

      return fAlloToSubmit;
    }
</script>

and on code behind you set that on the form.

protected void Page_Load(object sender, EventArgs e)
{
   Page.Form.Attributes["onsubmit"] = "return AllowFormToRun();";
}

And now for the UpdatePanel, you open that flag the moment you go for update, eg the moment you click that command as:

<script>
var prm = Sys.WebForms.PageRequestManager.getInstance();    
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);

function InitializeRequest(sender, args) {
   // here is run when you click the command on the UpdatePanel      
   fAlloToSubmit = false;
}

function EndRequest(sender, args) {
   // here is come after the upadate of the command
   fAlloToSubmit = true;
}
</script>

The trick here is that I hold the form to submit and not looking every control on page to disable it.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • @MorarMihai This code works. If you like it adapt it, or improve it. – Aristos Feb 27 '13 at 20:01
  • you mean that Page.Form.Attributes["onsubmit"] takes the button submitted even there might be more buttons (e.g. InsertButton, EditButton, CancelButton, etc). So the code must work for all kind of buttons? The script i can put it in the same file? – Morar Mihai Feb 28 '13 at 06:52
  • @MorarMihai The form is keep the full submit of the page, meaning that is hold all kind of buttons, any type of submit, even the javascript calls. Yes the script is go together with the page. – Aristos Feb 28 '13 at 07:12
  • 1
    the code is working fine, thank you Aristos. how do you find those answers? – Morar Mihai Feb 28 '13 at 17:51
  • uau, awesome. Could you give me some tutorials on this asp tags? – Morar Mihai Feb 28 '13 at 19:19
  • @MorarMihai I read the www.asp.net and check a lot the www.codeproject.com – Aristos Feb 28 '13 at 19:28