Link to home
Create AccountLog in
Avatar of jrhundsr
jrhundsrFlag for United States of America

asked on

.NET disable a submit button on click and validate the page

I have a button below that does almost what I want it to do.  But if the page does not pass the validation if stays disabled.  Is there a way to reenable the button if the page was not submitted?

<asp:Button ID="SubmitButton1" runat="server" BackColor="Navy" ForeColor="White"
                            Height="50px" Text="Save Shifts" UseSubmitBehavior="false"  OnClientClick="this.disabled=true;this.value='Please Wait...';" Width="100px" OnClick="SubmitButton1_Click" />
Avatar of Jeremy Daley
Jeremy Daley
Flag of United States of America image

since it looks like your validation is being done on the server via postback, couldn't you just say in your code (assuming C#) (psuedo-code):

if (passedValidation) {
    SubmitButton1.Enabled = true;
Avatar of jrhundsr


It's not being done on the server.  It is being done client side.
Avatar of Jeremy Daley
Jeremy Daley
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
i don't see how it's validating on client-side... you're only telling the button to disable itself and change its text onClientClick.

is your validation in the "SubmitButton1_Click" function?

i'm missing where your making an outreach to client-side script for validation.
The validator is just checking to see if there is a date in a box and if an item was selected out of a dropdown.   Like this:
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
                                runat="server" ControlToValidate="programddl" ErrorMessage="RequiredFieldValidator"
                                InitialValue="Please Select a Program">**</asp:RequiredFieldValidator>
Any thoughts?
Ok I decided to do the validation client side and I used this code in the submit button:
OnClientClick="this.disabled=true;this.value='Please Wait...';
The author's last comment shouldn't be accepted as the solution, for one. The code snippet posted in that comment still doesn't address the issue he originally posted (which I believe I answered). It is only including the "OnClientClick" code from his original question... DISABLING the button.

The author's original question was specifically this:
"Is there a way to reenable the button if the page was not submitted?"

I gave both a server-side AND client-side solution to this question.
I don't think you answered my question.  You did it as if I was validating in the code.  I was trying to validate with the visual studio validators.  My question was never fully answered so I went with another way because I need this project done.
your question was pretty simple and specific... how to re-enable a button.

"You did it as if I was validating in the code":
the code that you use to validate was never brought to my attention, nor did it need to be. i simply gave you an example of how to re-enable a button, either with javascript (client-side)... or C# (server-side). it doesn't matter HOW you choose to validate, you're gonna have to use javascript or C# to eventually get that button enabled again if you disabled with code in the first place (javascript).

i don't want to create a big fuss over some points, but you have to realize that experts use their own time to answer your questions. i would at least request that another expert review and advise.
Avatar of modus_operandi

Force accepted.
EE Moderator