views:

452

answers:

2

I have a User Control with form items that is re-used considerably throughout a large web application, and up until this point the validation summary upon an invalid form submission is being handled by the .aspx that consumes the User Control.

Now I need to set the ValidationGroup property at runtime for each of my form items controls (textboxes, lists, validators, etc). Rather than do it manually by setting each control I'm interested in iterating through all the controls in the User Control, detecting if that control has a ValidationGroup property, and setting it's value that way.

Something like this:

For Each ctrl As System.Web.UI.Control In Me.Controls
   ' so now what is the proper way to detect if this control has the ValidationGroup property
Next

Code sample in vb.net or c# works for me. Many thanks!

+2  A: 

Your UserControl should expose a property that sets the ValidationGroup property properly inside itself.

Control Markup in .ASPX:

<ctl:yourcontrol id="whatever" runat="server" YourValidationGroupProp="HappyValidationName" />

Control Code-behind .ASCX:

 protected override void OnPreRender(EventArgs e)
 {
     someControl.ValidationGroup = YourValidationGroupProp;
     someControl1.ValidationGroup = YourValidationGroupProp;
     someControl2.ValidationGroup = YourValidationGroupProp;
     //......etc
 }    

 public string YourValidationGroupProp{ get; set; }
rick schott
Rick this user control has approx 15 different form items, and about 10 different validation controls (~25 controls that have a ValidationGroup property). I am interested in looping through the collection of controls inside the UC, detecting if it has the ValidationGroup property and setting it there. I understand I can override each and every control as you suggest but was looking for a way to do it in one shot. Any thoughts on this?
Steve Flook
If this is a truly a re-used control you only have to implement the setters for those form elements one time in the code-behind as I stated.
rick schott
A: 

hi! create a custom control inheriting, for example, literal

this control will be a helper.

you will insert it in a page, have it do all the dirty work for ya, e.g. output code [which would take a lot of time to write] based on some logic and once you are done with it get that automatic code [which would be heavy weight to have that actually been done every time by another control], remove the helper control and hard-code-place the new code wherever you desire.

in this way you avoid all mistakes through having the computer figure out the code you want as wish and you gain all the hard-coded speed which would suffer by solving the problem with generic method.

I was just in the search of the same thing and it suddenly struck me. I use this method for other things [scan all controls & output some init code] but I guess you could use this method to easily do this as well!

..

I just wrote it and I'm gonna share it with you

public class ValidationCodeProducerHelper : Literal
{
    // you can set this in the aspx/ascx as a control property
    public string MyValidationGroup { get; set; }

    // get last minute controls
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        // start scanning from page subcontrols
        ControlCollection _collection = Page.Controls;
        Text = GetCode(_collection).Replace("\r\n", "<br/>");
    }

    private string GetCode(Control _control)
    {
        // building helper
        StringBuilder _output = new StringBuilder();

        // the logic of scanning
        if (_control.GetType().GetProperty("ValidationGroup") != null && !string.IsNullOrEmpty(_control.ID))
        {
            // the desired code
            _output.AppendFormat("{0}.{1} = {2};", _control.ID, "ValidationGroup", MyValidationGroup);
            _output.AppendLine();
        }

        // recursive search within children
        _output.Append(GetCode(_control.Controls));

        // outputting
        return _output.ToString();
    }

    private string GetCode(ControlCollection _collection)
    {
        // building helper
        StringBuilder _output = new StringBuilder();
        foreach (Control _control in _collection)
        {
            // get code for each child
            _output.Append(GetCode(_control));
        }
        // outputting
        return _output.ToString();
    }
}

God bless you

dl