Best way to globally install each ValidationGroup control in asp.net?

I have a user control with form elements that are significantly reused in a large web application, and until now, the validation summary for an invalid form submission is handled by .aspx, which uses the user control.

Now I need to set the ValidationGroup property at runtime for each control of the form elements (text fields, lists, validators, etc.). Instead of doing this manually by setting each control, I am interested in repeating all the controls in User Control, discovering whether this element has a ValidationGroup property and sets it this way.

Something like that:

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

Sample code in vb.net or C # works for me. Many thanks!

+3
source share
2 answers

The UserControl user must provide a property that correctly sets the ValidationGroup property within itself.

Check marking in .ASPX:

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

The 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; }
+1
source

Create your own control that inherits, for example, a literal. This control will be an assistant.

You enter it on the page, do all the dirty work for you. for example, the output code [which will take a long time to write] based on some logic and after you are done with it.

( , ), , , .

, , , , - , , .

, . [ ], , , !

, .

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();
    }
}
+1

Source: https://habr.com/ru/post/1717694/


All Articles