c# - Looping through children controls inside Updatepanel -
i have written method loops through properties of object , maps them controls have same name (or prefix + name). issue have controls inside update panel (drop down lists change when different option selected) not being found when running through method. read this , adapted method below accommodate that, still not find controls inside update panel. controls have ids , 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 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 updatepanel ? ((updatepanel)c).contenttemplatecontainer.hascontrols() : c.hascontrols()) { obj.mapobjecttopage(c); } } }
try add 2 methods 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 => label || literal || button || imagebutton || gridview || hyperlink || tabcontainer || dropdownlist || 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 getallcontrols method updatepanel parameter (or main container). method returns children of 'control' parameter. also, can remove clause retrieve controls (not of type).
i hope you!
Comments
Post a Comment