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