The button is only disabled if client-side validation passes. If client-side validation passes then the button is disabled and the server-side button click event is fired. The disabled status (along with the textual change on the button's value property)
will go away after the server-side code runs and the page finishes its postback.
I can't see how that will work as
1. You're disabling it AFTER the server-side button event, so it is not going to take affect until AFTER the next button click.
2. What is the use of checking for client-side validation AFTER the server-side event has happened, as it would have already happened for the server-side event to fire.
This will work, though I agree with Josh that disabling buttons could cause problems.
The button is not disabled AFTER the server-side button event. It is disabled BEFORE the server-side button event. When the button is clicked, here is the order of operation for this function:
If client-side validation DOES NOT exist or if client-side validation DOES exist AND client-side validation passes, then...
Disable the button client-side
Change button text
DO POSTBACK which executes the server-side button event code
The button will remain disabled until the server-side code post back is complete.
I've included a very basis .NET 1.1 example below. You can test it out easily. I put a 2.5 second sleep in the button server-side code to demonstrate the temporary disabling. You can keep or remove the RequiredFieldValidator on the page as it works with
or without client-side validation.
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace TestSolution { /// <summary> /// Summary description for WebForm1. /// </summary>public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1; protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e) { DisableButtonOnClientClick(ref this.Button1, "...Loading..."); } private void DisableButtonOnClientClick(ref Button control, string message) { control.Attributes["onclick"] = "if(typeof(Page_ClientValidate)=='undefined'||(typeof(Page_ClientValidate)=='function'&&Page_ClientValidate())){this.disabled=true;" + (message != null ? "this.value='" + message + "';" : "") + GetPostBackEventReference(control) + ";return false;}"; } private void DisableButtonOnClientClick(ref Button control) { DisableButtonOnClientClick(ref control, null); } #region Web Form Designer generated codeoverride protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. //
InitializeComponent();
base.OnInit(e); }
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary>private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Button1_Click(object sender, System.EventArgs e) { if (Page.IsValid) { System.Threading.Thread.Sleep(2500); } } } }
Sorry, in your original post I missed the call to DisableButtonOnClientClick in the Page_Load event handler which will make your code operate almost identical to what I posted.
What if you have your own custom validation on the form onsubmit? If that validation fails, you are left there with a disabled button and no way to submit the form.
Ho hum. Here is the same example page with a custom validator on it. If custom validation fails it does not leave the button disabled. Whether the custom validation is client-side or server-side it doesn't matter. Are you even testing the code or should
I just stop providing examples?
Dude, I don't need to test the code becuase, as mentioned before, I have already been down this path!
I am not talking about this CustomValidator bullcrap. I am talking about hard coding your own form onsubmit validation. Not with any .NET validators or anything like that, that is all rubbish. In several of my web apps, I have my own validation that I
put on the form onsubmit that does many different things. Your code will break that, guaranteed. I can say that with confidence withotu testing. Perhaps you should test your code with that and see for yourself!
I am done arguing with you about this. It is a bad idea to disable the submit button. Period.
JoshStodola
Star
13736 Points
3177 Posts
Re: Javascript calling c# function
Sep 19, 2007 07:54 PM|LINK
Based on my own experience, Im not so sure disabling the submit button is a good idea. It has caused so many unexpected problems in the past.
What if you have validation on your form onsubmit?
jamezw
Participant
1211 Points
190 Posts
Re: Javascript calling c# function
Sep 19, 2007 08:01 PM|LINK
The button is only disabled if client-side validation passes. If client-side validation passes then the button is disabled and the server-side button click event is fired. The disabled status (along with the textual change on the button's value property) will go away after the server-side code runs and the page finishes its postback.
JoshStodola
Star
13736 Points
3177 Posts
Re: Javascript calling c# function
Sep 19, 2007 08:04 PM|LINK
No need to repeat yourself. I'd love to see an in-production example of this...
jamezw
Participant
1211 Points
190 Posts
Re: Javascript calling c# function
Sep 19, 2007 08:09 PM|LINK
Unfortunately I cannot provide links to my company's internal systems [:)]
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: Javascript calling c# function
Sep 20, 2007 12:04 AM|LINK
James,
I can't see how that will work as
1. You're disabling it AFTER the server-side button event, so it is not going to take affect until AFTER the next button click.
2. What is the use of checking for client-side validation AFTER the server-side event has happened, as it would have already happened for the server-side event to fire.
This will work, though I agree with Josh that disabling buttons could cause problems.
private void Page_Load(object sender, System.EventArgs e)
{
StringBuilder javaScript = new StringBuilder();
javaScript.Append("if ( typeof(Page_ClientValidate) == 'function' )");
javaScript.Append("{ ");
javaScript.Append(" if ( Page_ClientValidate() == false )");
javaScript.Append(" return false;");
javaScript.Append("} ");
javaScript.Append("this.value = 'Please wait...';");
javaScript.Append("this.disabled = true;");
javaScript.Append(Page.GetPostBackEventReference(Button1, string.Empty));
javaScript.Append(";");
Button1.Attributes.Add("onclick", javaScript.ToString());
}
private void Button1_Click(object sender, System.EventArgs e)
{
// Emulate a lengthy process...
TimeSpan waitTime = new TimeSpan(0, 0, 0, 10);
System.Threading.Thread.Sleep(waitTime);
Response.Write("Button1_Click fired<br>");
}
NC...
jamezw
Participant
1211 Points
190 Posts
Re: Javascript calling c# function
Sep 20, 2007 01:08 PM|LINK
The button is not disabled AFTER the server-side button event. It is disabled BEFORE the server-side button event. When the button is clicked, here is the order of operation for this function:
The button will remain disabled until the server-side code post back is complete.
I've included a very basis .NET 1.1 example below. You can test it out easily. I put a 2.5 second sleep in the button server-side code to demonstrate the temporary disabling. You can keep or remove the RequiredFieldValidator on the page as it works with or without client-side validation.
ASPXASPX.cs
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: Javascript calling c# function
Sep 20, 2007 01:30 PM|LINK
Sorry, in your original post I missed the call to DisableButtonOnClientClick in the Page_Load event handler which will make your code operate almost identical to what I posted.
NC...
JoshStodola
Star
13736 Points
3177 Posts
Re: Javascript calling c# function
Sep 20, 2007 01:42 PM|LINK
Just like I already mentioned....
What if you have your own custom validation on the form onsubmit? If that validation fails, you are left there with a disabled button and no way to submit the form.
jamezw
Participant
1211 Points
190 Posts
Re: Javascript calling c# function
Sep 20, 2007 02:06 PM|LINK
Ho hum. Here is the same example page with a custom validator on it. If custom validation fails it does not leave the button disabled. Whether the custom validation is client-side or server-side it doesn't matter. Are you even testing the code or should I just stop providing examples?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebForm1</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <script type="text/javascript"> function doCustomValidation(sender,args) { args.IsValid = (document.getElementById('<%=TextBox1.ClientID%>').value != ''); } </script> </HEAD> <body> <form id="Form1" method="post" runat="server"> <asp:TextBox id="TextBox1" runat="server" /> <asp:Button id="Button1" runat="server" Text="Submit" /> <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="Field is required." ControlToValidate="TextBox1" /> <asp:CustomValidator ID="CustomValidator1" Runat="server" ErrorMessage="Custom validation failed." ClientValidationFunction="doCustomValidation" /> </form> </body> </HTML>JoshStodola
Star
13736 Points
3177 Posts
Re: Javascript calling c# function
Sep 20, 2007 02:10 PM|LINK
Dude, I don't need to test the code becuase, as mentioned before, I have already been down this path!
I am not talking about this CustomValidator bullcrap. I am talking about hard coding your own form onsubmit validation. Not with any .NET validators or anything like that, that is all rubbish. In several of my web apps, I have my own validation that I put on the form onsubmit that does many different things. Your code will break that, guaranteed. I can say that with confidence withotu testing. Perhaps you should test your code with that and see for yourself!
I am done arguing with you about this. It is a bad idea to disable the submit button. Period.
Regards...