Nimble Coder

Adventures in Nimble Coding
posts - 77, comments - 56, trackbacks - 1

Disabling Fields on Submit

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
    {
        /// <summary>
        /// Adds client-side javascript code to disable controls on a submit.
        /// </summary>
        /// <param name="Page">Reference to the current page</param>
        /// <param name="strControls">String array of client-side control names.</param>
        public static void DisableCtrlOnSubmit(
            System.Web.UI.Page Page, 
            params string[] strControls)
        {
            StringBuilder sbJS = new StringBuilder(200);
            sbJS.Append(@"
<script language=""javascript"">
function MSDisable() {
    try {
        if (Page_IsValid==false) return;
        window.setTimeout('MSDisableCtrls(true)',0);
    } catch (e) {
        window.setTimeout('MSDisableCtrls(true)',0);
    }
}

function MSEnable() {
    try {
        window.setTimeout('MSDisableCtrls(false)',0);
    } catch (e) {
        window.setTimeout('MSDisableCtrls(false)',0);
    }
}
"
                );

            sbJS.Append(@"
function MSDisableCtrls(value) {
"
                );

            foreach (string strControl in strControls)
            {
                sbJS.Append("\ttry { ");
                sbJS.Append(strControl);
                sbJS.Append(".disabled = value;");
                sbJS.Append(" } catch (e) {}\n");
            }

            sbJS.Append(@"
}
</script>
"
                );

            if (!Page.IsClientScriptBlockRegistered("MSDisableImpl"))
            {
                Page.RegisterClientScriptBlock(
                    "MSDisableImpl", 
                    sbJS.ToString());
            }

            Page.RegisterOnSubmitStatement("MSDisable", "MSDisable(false);");
        }

        /// <summary>
        /// Adds client-side javascript code to disable controls on a submit.
        /// </summary>
        /// <param name="Page">Reference to the current page</param>
        /// <param name="aryClientControlNames">ArrayList with the client-side control names</param>
        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);
        }
    }
}

Print | posted on Tuesday, May 01, 2007 10:31 PM | Filed Under [ C# ASP.NET ]

Feedback

Gravatar

# re: Disabling Fields on Submit

Hi there
How funny to find this article from MSDN Magazine, my old stomping ground, quoted here. I just want to say, although I can't remember who wrote this utility, it was not me.
--Nancy Michell
4/12/2010 3:17 PM | NANCY
Gravatar

# re: Disabling Fields on Submit

@Nancy, I remembered trying to figure out who originally wrote the code and decided to list you although the article said, "Edited by Nancy Michel," and never credited anyone specifically for the code. Thank you for the clarification.
4/13/2010 10:14 PM | ryanvs

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 2 and 5 and type the answer here:

Powered by: