A while back I needed to disable fields on a web page during submit and I found a utility written by Nancy Michell for MSDN Magazine. The utility covered the basic cases, but I found that I needed to use it for AJAX style calls as well as standard submits. So I modified the code to include an enable function as well. Finally I added exception handling around the disable and enable code to avoid pesky Javascript errors. The only thing I might change is to capture an array of the controls that were successfully disabled and then process only those controls on enable.
To use the disable utility, do the following:
// Method 1: Using the param string[] for controls DisableHelper.DisableCtrlOnSubmit(this, "ServerForm.btnCancel", "ServerForm.btnSave"); // Method 2: Using the ArrayList of controls aryDisableControls = new ArrayList(); aryDisableControls.Add("ConfigLoopForm.btnCancel"); aryDisableControls.Add("ConfigLoopForm.btnSave"); DisableHelper.DisableCtrlOnSubmit(this, aryDisableControls);
Here is the utility source:
using System; using System.Collections; using System.Text; namespace Utility.Web.UI { public class DisableHelper { ////// Adds client-side javascript code to disable controls on a submit. /// /// Reference to the current page /// String array of client-side control names. public static void DisableCtrlOnSubmit( System.Web.UI.Page Page, params string[] strControls) { StringBuilder sbJS = new StringBuilder(200); sbJS.Append(@" " ); if (!Page.IsClientScriptBlockRegistered("MSDisableImpl")) { Page.RegisterClientScriptBlock( "MSDisableImpl", sbJS.ToString()); } Page.RegisterOnSubmitStatement("MSDisable", "MSDisable(false);"); } ////// Adds client-side javascript code to disable controls on a submit. /// /// Reference to the current page /// ArrayList with the client-side control names public static void DisableCtrlOnSubmit( System.Web.UI.Page Page, ArrayList aryClientControlNames) { if (aryClientControlNames != null) { string[] strControls = (string[]) aryClientControlNames.ToArray(typeof(string)); DisableCtrlOnSubmit(Page, strControls); } } private static void DiableCtrlOnSubmit( System.Web.UI.Page Page, ArrayList aryClientControlNames) { DisableCtrlOnSubmit(Page, aryClientControlNames); } } }
Comments are closed.