Validation groups in User Controls

 

Found a neat way of enabling validation groups in user controls.

http://aspalliance.com/1741_Video_Using_ValidationGroup_Control_with_User_Controls

I made a slight change for this to work with panels. Had to iterate through child controls to get the ValidationGroups set correctly.

using System;

using System.Reflection;

using System.Web.UI;

 

    public class BaseUserControl : UserControl

    {

        public BaseUserControl()

        {

            this.Load += new EventHandler(BaseUserControl_Load);

        }

 

        void BaseUserControl_Load(object sender, EventArgs e)

        {

            AssignValidation(this);

        }

 

        public string ValidationGroup

        {

            get { return (string)ViewState["ValidationGroup"]; }

            set { ViewState["ValidationGroup"] = value; }

        }

 

 

        protected void AssignValidation(Control ParentControl)

        {

            foreach (Control control in ParentControl.Controls)

            {

                AssignValidation(control);

 

                PropertyInfo property = control.GetType().GetProperty("ValidationGroup");

               

                if (property == null) continue;

 

                property.SetValue(control, ValidationGroup, null);

            }

 

        }

    }

 


-
Published 15 January 2009 12:06 by simonsabin
Filed under:

Comments

No Comments