Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Updatepanel内的子控件之间循环_C#_Asp.net_Reflection - Fatal编程技术网

C# 在Updatepanel内的子控件之间循环

C# 在Updatepanel内的子控件之间循环,c#,asp.net,reflection,C#,Asp.net,Reflection,我编写了一个方法,该方法循环遍历对象的所有属性,并将它们映射到具有相同名称(或前缀+名称)的控件。问题是,我在更新面板(当选择不同选项时会更改的下拉列表)中有某些控件,在运行此方法时找不到这些控件。我阅读并调整了下面的方法以适应这种情况,但它仍然无法在更新面板中找到控件。所有控件都有ID和runat=“server” 公共静态void MapObjectToPage(此对象对象对象,控件父对象,字符串前缀=”) { Type Type=obj.GetType(); Dictionary prop

我编写了一个方法,该方法循环遍历对象的所有属性,并将它们映射到具有相同名称(或前缀+名称)的控件。问题是,我在更新面板(当选择不同选项时会更改的下拉列表)中有某些控件,在运行此方法时找不到这些控件。我阅读并调整了下面的方法以适应这种情况,但它仍然无法在更新面板中找到控件。所有控件都有ID和runat=“server”

公共静态void MapObjectToPage(此对象对象对象,控件父对象,字符串前缀=”)
{
Type Type=obj.GetType();
Dictionary props=type.GetProperties(BindingFlags.Public | BindingFlags.Instance).ToDictionary(info=>prefix+info.Name.ToLower());
ControlCollection theControls=parent是UpdatePanel?((UpdatePanel)parent).ContentTemplateContainer.Controls:parent.Controls;
foreach(控件中的控件c)
{
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)props[key].GetValue(obj,null)).ToSortDateString()
: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)props[key].GetValue(obj,null)).ToSortDateString()
:props[key].GetValue(obj,null).ToString();
}
//剪断!
}
如果(c)是UpdatePanel
((UpdatePanel)c).ContentTemplateContainer.HasControls()
:c.HasControls())
{
对象映射对象页(c);
}
}
}

尝试将这两个方法添加到新类或现有类中:

公共静态列表(此控件)
{
var children=control.Controls.Cast();
返回children.SelectMany(c=>flattchildren(c)。其中(a=>a是标签| | | a是文字| | | a是按钮| | | a是图像按钮| | | a是网格视图| | a是超链接| a是选项卡容器| | | a是下拉列表| | | | a是面板)).Concat(children;
}
公共静态列表GetAllControls(控件)
{
var children=control.Controls.Cast();
返回children.SelectMany(c=>flattchildren(c)).Concat(children.ToList();
}
可以使用updatePanel作为参数(或主容器)调用GetAllControls方法。该方法返回“control”参数的所有子级。此外,还可以删除Where子句以检索所有控件(而不是特定类型的控件)


我希望这对你有帮助

UpdatePanel是直接访问的。您不需要/无法使用FindControl来查找它。用这个

foreach (Control ctrl in YourUpdatepanelID.ContentTemplateContainer.Controls)
{
    if (ctrl.GetType() == typeof(TextBox))
        ((TextBox)(ctrl)).Text = string.Empty;
    if (ctrl.GetType() == typeof(CheckBox))
        ((CheckBox)(ctrl)).Checked = false;
} 

从这个答案中我唯一有问题的是多重枚举警告。我添加了这个,并在return语句中用enumeratedChildren替换了children,效果很好:IEnumerable enumeratedChildren=作为控件的children[]??儿童。ToArray();
    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();
    }
foreach (Control ctrl in YourUpdatepanelID.ContentTemplateContainer.Controls)
{
    if (ctrl.GetType() == typeof(TextBox))
        ((TextBox)(ctrl)).Text = string.Empty;
    if (ctrl.GetType() == typeof(CheckBox))
        ((CheckBox)(ctrl)).Checked = false;
}