Thursday, 29 August 2013

Prevent field validators from firing when editing a web part

I have a simple visual web part with a RequiredFieldValidator. The only problem is that when the web part properties are set, a postback is fired and the validator is complaining. The net result is that I cannot make any configuration changes to the web part.

The following code will resolve this problem:

    protected override void OnPreRender(EventArgs e)
        {
            WebPartManager mgr = WebPartManager.GetCurrentWebPartManager(Page);
            if (mgr.DisplayMode.Equals(mgr.SupportedDisplayModes["Browse"]))
            {
                myValidator.Enabled = true;
            }
            else
            {
                myValidator.Enabled = false;
            }

            base.OnPreRender(e);
        }

No comments:

Post a comment