Best way to globally set every control's ValidationGroup property in an asp.net?
-
07-07-2019 - |
Question
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!
Solution
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; }
OTHER TIPS
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 you. 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 going to 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();
}
}