Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top