【问题标题】:Best way to globally set every control's ValidationGroup property in an asp.net?在 asp.net 中全局设置每个控件的 ValidationGroup 属性的最佳方法?
【发布时间】:2009-09-15 00:41:02
【问题描述】:

我有一个包含表单项的用户控件,该控件在整个大型 Web 应用程序中被大量重复使用,直到此时,无效表单提交的验证摘要由使用用户控件的 .aspx 处理。

现在我需要在运行时为每个表单项控件(文本框、列表、验证器等)设置 ValidationGroup 属性。而不是通过设置每个控件来手动进行,我感兴趣的是遍历用户控件中的所有控件,检测该控件是否具有 ValidationGroup 属性,并以这种方式设置它的值。

类似这样的:

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

vb.net 或 c# 中的代码示例适用于我。非常感谢!

【问题讨论】:

  • 我真的在寻找一种方法来遍历所有控件并确定哪些控件包含 ValidationGroup 属性。经过一些进一步的研究,似乎 Type.GetField() 将允许我检测哪些控件具有 ValidationGroup 属性,但随后您需要转换为适当的类型、设置属性等。我想这是其中之一实施动态方法所需的总工作量超过了更直接/手动的方法。我有大约 35 个控件,只是按照 Rick 的建议进行设置。

标签: asp.net webforms web-controls


【解决方案1】:

您的 UserControl 应该公开一个在其内部正确设置 ValidationGroup 属性的属性。

.ASPX 中的控制标记:

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

控制代码隐藏 .ASCX:

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

 public string YourValidationGroupProp{ get; set; }

【讨论】:

  • Rick 这个用户控件有大约 15 个不同的表单项,以及大约 10 个不同的验证控件(大约 25 个具有 ValidationGroup 属性的控件)。我有兴趣遍历 UC 内部的控件集合,检测它是否具有 ValidationGroup 属性并将其设置在那里。我知道我可以按照您的建议覆盖每个控件,但正在寻找一种方法来一次性完成。对此有什么想法吗?
  • 如果这是一个真正可重复使用的控件,您只需按照我所说的在代码隐藏中实现一次这些表单元素的设置器。
【解决方案2】:

创建一个自定义控件继承,例如,文字。这个控件将是一个助手。

你将把它插入一个页面,让它为你做所有的脏活。 例如根据一些逻辑输出代码[这将花费大量时间来编写],一旦你完成它。

获取该自动代码(如果每次都由另一个控件实际完成,这将是沉重的负担),删除辅助控件并将新代码硬编码放置在您想要的任何位置。

通过这种方式,您可以通过让计算机根据需要计算出您想要的代码来避免所有错误,并且您可以获得所有硬编码的速度,而这会因使用通用方法解决问题而受到影响。

我只是在寻找同样的东西,它突然打动了我。 我将此方法用于其他事情[扫描所有控件并输出一些初始化代码],但我想您也可以使用此方法轻松做到这一点!

我刚刚写了,我要分享给你

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

【讨论】:

    猜你喜欢
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    相关资源
    最近更新 更多