0

I am new to C#. I have a save button inside InsertItemTemplate. I have used the following code to disable the button after first click in java script but its not even working for the first click please help me.

<asp:ImageButton ID="imgbtnSave" runat="server" CommandName="Add" CausesValidation="true" OnClientClick="this.disabled='true';return true;"  />
3
  • I think the value in "OnClientClick" has to be a method in your C# code. You would then add the code to disable the button there, such as: imgbtnSave.Enabled = false; Commented Apr 5, 2016 at 18:02
  • Thank you for your response i want to stop multiple postbacks Commented Apr 5, 2016 at 18:17
  • 1
    @B.ClayShannon you are thinking of the OnClick atribute. OnClientClick essentially takes whatever you put in there and appends it to what ASP.NET puts in the onclick HTML attribute. Commented Apr 7, 2016 at 18:02

1 Answer 1

1

You are modifying the "disabled" property of the DOM object on the browser, but the button will do a post back to the server when it's clicked, so any change to the DOM will be lost.

On the function where you handle the command "Add" in your server code you must retrieve the button from the InsertItemTemplate and set its "Enabled" property to false, that will disable the control from the server side.

If you want to avoid multiple clicks while the page has not been reloaded then you need a client function to avoid this, something like this:

<asp:ImageButton ID="imgbtnSave" runat="server" CommandName="Add" CausesValidation="true" OnClientClick="return checkEnabled(this);"  />

<!-- somewhere in your page -->
<script>
    function checkEnabled(item)
    {
        if(item.disabled != 'true')
        {
            item.disabled = 'true';
            return true;
        }

        return false;
    }
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much.. I did try it but the function is being called twice if i hit save button twice. How do i stop it
Mix both solutions, OnClientClick set OnClientClick="if(this.disabled != 'true'){this.disabled='true';return true;}else{ return false; }" and the server part remains the same
Thank you .. I called the function using this keyword to access image button from list view but its allowing multiple post backs OnClientClick= "checkEnabled(this)"
YOu missed the "return", that's what will block the second post back

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.