通过孩子里面循环UpdatePanel控件 [英] Looping through children controls inside Updatepanel

查看:142
本文介绍了通过孩子里面循环UpdatePanel控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了通过对象的所有属性循环,并将它们映射到具有相同的名称(或preFIX +名)控制的方法。问题是,我有一个更新面板(下拉当不同的选项被选中了更改列表),通过此方法运行时,不会被人发现里面的某些控制。我读并适应下面的方法以适应这一点,但它仍然不会找到更新面板内部的控制。所有控件具有ID和=服务器。

 公共静态无效MapObjectToPage(这obj对象,控制父,字符串preFIX =)
{
    类型type = obj.GetType();
    字典<字符串的PropertyInfo>道具= type.GetProperties(BindingFlags.Public | BindingFlags.Instance).ToDictionary(资讯= GT; preFIX + info.Name.ToLower());    的ControlCollection theControls =父的UpdatePanel? ((的UpdatePanel)母公司).ContentTemplateContainer.Controls:parent.Controls;    的foreach(在theControls控制C)
    {
        如果(props.Keys.Contains(c.ClientID.ToLower())及与放大器;!道具[c.ClientID.ToLower()]的GetValue(物镜,空)=空)
        {
            字符串键= c.ClientID.ToLower();
            如果(c.GetType()== typeof运算(文本框))
            {
                ((文本框)C)。文本=道具[关键] .PropertyType == typeof运算(日期时间?)
                    ||道具[关键] .PropertyType == typeof运算(DATETIME)
                    ? ((日期时间)的道具[关键] .GetValue(OBJ,NULL))。ToShortDateString()
                    :道具[关键] .GetValue(OBJ,空)的ToString();
            }
            否则,如果(c.GetType()== typeof运算(的HtmlInputText))
            {
                ((的HtmlInputText)C).value的道具= [关键] .PropertyType == typeof运算(日期时间?)
                    ||道具[关键] .PropertyType == typeof运算(DATETIME)
                    ? ((日期时间)的道具[关键] .GetValue(OBJ,NULL))。ToShortDateString()
                    :道具[关键] .GetValue(OBJ,空)的ToString();
            }
            //喀嚓!
        }
        如果(c是的UpdatePanel
        ? ((的UpdatePanel)C).ContentTemplateContainer.HasControls()
        :c.HasControls())
        {
            obj.MapObjectToPage(C);
        }
    }
}


解决方案

尝试将此两种方法添加到新的或现有的类:


 公共静态列表<控制> FlattenChildren(该控制的控制)
    {
        VAR孩子= control.Controls.Cast<控制>();
        返回children.SelectMany(C => FlattenChildren(C)。凡(A =>一种是标签||一个是文字||一个按钮是一个||是||的ImageButton一个GridView控件是一个||超链接是||一个是TabContainer的一个|| DropDownList的是一个||是面板))的毗连(儿童).ToList()。
    }


 公共静态列表<控制> GetAllControls(控制控制)
    {
        VAR孩子= control.Controls.Cast<控制>();
        返回children.SelectMany(C => FlattenChildren(C))的毗连(儿童).ToList()。
    }

您可以调用GetAllControls方法,在UpdatePanel作为参数(或主容器)。该方法返回控制参数的所有的孩子们。此外,您还可以删除Where子句来检索所有的控件(而不是那些特定类型的)。​​

我希望这会帮助你的!

I have written a method that loops through all the properties of an object and maps them to controls that have the same name (or prefix + name). The issue is that I have certain controls inside an update panel (drop down lists that change when a different option is selected) that are not being found when running through this method. I read this and adapted the method below to accommodate for that, but it still will not find the controls inside the update panel. All the controls have IDs and runat="server".

public static void MapObjectToPage(this object obj, Control parent, string prefix = "")
{
    Type type = obj.GetType();
    Dictionary<string, PropertyInfo> props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance).ToDictionary(info => prefix + info.Name.ToLower());

    ControlCollection theControls = parent is UpdatePanel ? ((UpdatePanel)parent).ContentTemplateContainer.Controls : parent.Controls;

    foreach (Control c in theControls)
    {
        if (props.Keys.Contains(c.ClientID.ToLower()) && props[c.ClientID.ToLower()].GetValue(obj, null) != null)
        {
            string key = c.ClientID.ToLower();
            if (c.GetType() == typeof(TextBox))
            {
                ((TextBox)c).Text = props[key].PropertyType == typeof(DateTime?)
                    || props[key].PropertyType == typeof(DateTime)
                    ? ((DateTime)props[key].GetValue(obj, null)).ToShortDateString()
                    : props[key].GetValue(obj, null).ToString();
            }
            else if (c.GetType() == typeof(HtmlInputText))
            {
                ((HtmlInputText)c).Value = props[key].PropertyType == typeof(DateTime?)
                    || props[key].PropertyType == typeof(DateTime)
                    ? ((DateTime)props[key].GetValue(obj, null)).ToShortDateString()
                    : props[key].GetValue(obj, null).ToString();
            }
            //snip!
        }
        if (c is UpdatePanel
        ? ((UpdatePanel)c).ContentTemplateContainer.HasControls()
        : c.HasControls())
        {
            obj.MapObjectToPage(c);
        }
    }
}

解决方案

Try to add this two methods to a new or existing class:

    public static List<Control> FlattenChildren(this Control control)
    {
        var children = control.Controls.Cast<Control>();
        return children.SelectMany(c => FlattenChildren(c).Where(a => a is Label || a is Literal || a is Button || a is ImageButton || a is GridView || a is HyperLink || a is TabContainer || a is DropDownList || a is Panel)).Concat(children).ToList();
    }

    public static List<Control> GetAllControls(Control control)
    {
        var children = control.Controls.Cast<Control>();
        return children.SelectMany(c => FlattenChildren(c)).Concat(children).ToList();
    }

You can call the GetAllControls method with the updatePanel as parameter (or the main container). The method returns all the children of the 'control' parameter. Also, you can remove the Where clause to retrieve all the controls (not those of a certain type).

I hope this will help you!

这篇关于通过孩子里面循环UpdatePanel控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆