Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Create a custom control inheriting, for example, literal. This control will be a helper.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>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 &amp; output some init code] but I guess you could use this method to easily do this as well!</p> <p>I just wrote it and I'm going to share it with you</p> <pre><code>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", "&lt;br/&gt;"); } private string GetCode(Control _control) { // building helper StringBuilder _output = new StringBuilder(); // the logic of scanning if (_control.GetType().GetProperty("ValidationGroup") != null &amp;&amp; !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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload