Refactor Code

  • Gravatar
    How to use ValidationGroup with user controls

    by azamsharp on 8/14/2008 8:10:12 PM
  • How do you use ValidationGroup control with the user controls? Like how do you make a user control behave like a unit and validate only that user control. I have used the following approach. Here is the post: http://azamsharp.com/Posts/77_Unraveling_the_Mysterious_of_the_ValidationGroup_Control.aspx
  • public class BaseUserControl : UserControl
    {
    public BaseUserControl()
    {
    // automatically register the event
    this.Load += new EventHandler(BaseUserControl_Load);
    }

    void BaseUserControl_Load(object sender, EventArgs e)
    {
    // something!
    AssignValidationGroupToControls();
    }

    // assign the validation group to the controls
    protected void AssignValidationGroupToControls()
    {
    // get the validation control from this control and assign the validation group
    foreach (Control control in this.Controls)
    {
    PropertyInfo property = control.GetType().GetProperty("ValidationGroup");
    if (property == null) continue;

    property.SetValue(control, ValidationGroup, null);
    }
    }

    public string ValidationGroup
    {
    get { return (string)ViewState["ValidationGroup"]; }
    set { ViewState["ValidationGroup"] = value; }
    }
    }
  • Refactor it!
Please log in to refactor the code! Login