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 is directly access. You don't need to/can't use FindControl to find it. Use this
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;
}
Related
I need a way to dynamically gather all of the TextBoxes inside of a custom UserContorl in ASP.net WebForms, server-side
I thought this would work:
foreach (var control in Page.Controls)
{
var textBox = control as TextBox;
if (textBox != null && textBox.MaxLength > 0)
{
// stuff here
}
}
But it doesn't do what I thought it would, and I don't see how else to get that information.
So, how can I dynamically get all of the textboxes on the server-side of a custom UserControl in ASP.net webforms?
You need a recursive method, because not all level 1 children are necessarily text boxes (depends on the control/container hierarchy in your user control):
private IEnumerable<TextBox> FindControls(ControlCollection controls)
{
List<TextBox> results = new List<TextBox>();
foreach(var control in controls)
{
var textBox = control as TextBox;
if (textBox != null && textBox.MaxLength > 0)
{
results.Add(textBox);
}
else if(textBox == null)
{
results.AddRange(FindControls(control.Controls));
}
}
return results;
}
After you get the results you can iterate them and do whatever you need to do.
Looks like recursive is the way to go:
foreach (Control control in Page.Controls)
{
DoSomething(control);
}
// And you need a new method to loop through the children
private void DoSomething(Control control)
{
if (control.HasControls())
{
foreach(Control c in control.Controls)
{
DoSomething(c);
}
}
else
{
var textBox = control as TextBox;
if (textBox != null)
{
// Do stuff here
}
}
}
I want to add runat=server dynamically to a CheckBoxList so that it can be found by FindControl.
CheckBoxList cbl = new CheckBoxList();
cbl.ID = "cbl" + intQuestionCount.ToString();
// get choices from choice list
int intChoiceListId = Convert.ToInt32(detail.ChoiceListID);
var choiceList = (from cl in _svsCentralDataContext.SVSSurvey_ChoiceListItems
where cl.ChoiceListID == intChoiceListId
orderby cl.Description
select cl);
cbl.DataSource = choiceList;
cbl.DataTextField = "Description";
cbl.DataBind();
cbl.Visible = true;
cbl.CssClass = "PositionCol3";
questionsPanel.Controls.Add(cbl);
I have 2 recursive find control methods:
private HtmlControl FindHtmlControlByIdInControl(Control control, string id)
{
foreach (Control childControl in control.Controls)
{
if (childControl.ID != null && childControl.ID.Equals(id, StringComparison.OrdinalIgnoreCase)
&& childControl is HtmlControl
)
{
return (HtmlControl)childControl;
}
if (childControl.HasControls())
{
HtmlControl result = FindHtmlControlByIdInControl(childControl, id);
if (result != null)
{
return result;
}
}
}
return null;
}
private WebControl FindWebControlByIdInControl(Control control, string id)
{
foreach (Control childControl in control.Controls)
{
if (childControl.ID != null && childControl.ID.Equals(id, StringComparison.OrdinalIgnoreCase)
&& childControl is WebControl
)
{
return (WebControl)childControl;
}
if (childControl.HasControls())
{
WebControl result = FindWebControlByIdInControl(childControl, id);
if (result != null)
{
return result;
}
}
}
return null;
}
The screen is initially created dynamically (if !isPostback), based on an SQL record. The FindControl methods are used after this lot has been displayed, when the user clicks the 'Save' button.
Neither Find control method finds my CheckBoxList!!
You are adding controls through your code behind, they are already server side controls, you don't have to add runat="server". You are not finding them properly.
Make sure they are added to the page before you look for them.
I've a number of Devexpress controls on my form under layoutcontrol1. I used
the code below to iterate through each control and clear the existing data when users click on "Clear" button. However, it doesn't work on LookupEdit (set the EditValue to 0) and CheckedListBoxControl (uncheck all the selected items).
foreach (Control c in layoutControl1.Controls)
{
if (c.GetType() == typeof(TextEdit) || c.GetType()==typeof(MemoEdit))
c.Text = String.Empty;
if (c.GetType() == typeof(LookUpEdit))
c.EditValue = 0; //doesn't have EditValue property
if (c.GetType() == typeof(CheckedListBoxControl))
c.CheckedItems = CheckState.Unchecked; //doesn't have such property
}
Any suggestion?
Just try the following:
foreach(Control c in layoutControl1.Controls) {
var edit = c as DevExpress.XtraEditors.BaseEdit; // base class for DX editors
if(edit != null)
edit.EditValue = null;
var listBox = c as DevExpress.XtraEditors.CheckedListBoxControl;
if(listBox != null)
listBox.UnCheckAll();
}
I have a cached control object form a UserControl in asp.net cache.
I like to access my control and make a copy of it to freely change it without having any affect on the base cached control.
Hi
I found the code that I was looking for.
I put it here maybe it helps you too.
/// <summary>
/// this method makes a copy of object as a clone function
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static object Clone_Control(object o)
{
Type type = o.GetType();
PropertyInfo[] properties = type.GetProperties();
Object retObject = type.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, o, null);
foreach (PropertyInfo propertyInfo in properties)
{
if (propertyInfo.CanWrite)
{
propertyInfo.SetValue(retObject, propertyInfo.GetValue(o, null), null);
}
}
return retObject;
}
It is certainly possible to do what you are asking to do. And it is very straightforward. Something like this maybe?
private static void ChangeMyControlTextFromCache(string controlName, string newText, Panel containingPanel)
{
MyControl original = ViewState[controlName] as MyControl;
if (original == null)
{
original = new MyControl();
ViewState[controlName] = original;
}
MyControl clone = CloneMyControl(original);
clone.Text = newText;
if (containingPanel.Children.Contains(original))
containingPanel.Children.Remove(original);
containingPanel.Children.Add(clone);
}
private static MyControl CloneMyControl(MyControl original)
{
MyControl clone = new MyControl();
clone.Text = original.Text;
clone.SomeOtherProperty = original.SomeOtherProperty;
return clone;
}
Here is another clone method that will also work with MONO.
It clones the passed control and it's children.
Note: A cloned control cannot be added to a different page (or in a later post-back) even it has not been added to any page yet. It might seem to work on Windows but will crash on MONO so I guess it is generally not meant to be done.
public static Control CloneControl(Control c) {
var clone = Activator.CreateInstance(c.GetType()) as Control;
if (c is HtmlControl) {
clone.ID = c.ID;
foreach (string key in ((HtmlControl)c).Attributes.Keys)
((HtmlControl)clone).Attributes.Add(key, (string)((HtmlControl)c).Attributes[key]);
}
else {
foreach (PropertyInfo p in c.GetType().GetProperties()) {
// "InnerHtml/Text" are generated on the fly, so skip them. "Page" can be ignored, because it will be set when control is added to a Page.
if (p.CanRead && p.CanWrite && p.Name != "InnerHtml" && p.Name != "InnerText" && p.Name != "Page") {
try {
p.SetValue(clone, p.GetValue(c, p.GetIndexParameters()), p.GetIndexParameters());
}
catch {
}
}
}
}
for (int i = 0; i < c.Controls.Count; ++i)
clone.Controls.Add(CloneControl(c.Controls[i]));
return clone;
}
try this for page includes LiteralControl and ResourceBasedLiteralControl.
public static System.Web.UI.Control CloneControl(Control c)
{
Type type = c.GetType();
var clone = (0 == type.GetConstructors().Length) ? new Control() : Activator.CreateInstance(type) as Control;
if (c is HtmlControl)
{
clone.ID = c.ID;
AttributeCollection attributeCollection = ((HtmlControl)c).Attributes;
System.Collections.ICollection keys = attributeCollection.Keys;
foreach (string key in keys)
{
((HtmlControl)c).Attributes.Add(key, (string)attributeCollection[key]);
}
}else if(c is System.Web.UI.LiteralControl){
clone = new System.Web.UI.LiteralControl(((System.Web.UI.LiteralControl)(c)).Text);
}
else
{
PropertyInfo[] properties = c.GetType().GetProperties();
foreach (PropertyInfo p in properties)
{
// "InnerHtml/Text" are generated on the fly, so skip them. "Page" can be ignored, because it will be set when control is added to a Page.
if (p.Name != "InnerHtml" && p.Name != "InnerText" && p.Name != "Page" && p.CanRead && p.CanWrite)
{
try
{
ParameterInfo[] indexParameters = p.GetIndexParameters();
p.SetValue(clone, p.GetValue(c, indexParameters), indexParameters);
}
catch
{
}
}
}
}
int cControlsCount = c.Controls.Count;
for (int i = 0; i < cControlsCount; ++i)
{
clone.Controls.Add(CloneControl(c.Controls[i]));
}
return clone;
}
ref:
Dynamic Web Controls, Postbacks, and View State
By Scott Mitchell
Upvoted answers didn't work for me. Here's my solution to creating a copy of an ASP.NET control object.
// Copies the custom properties on the control. To use it, cast your object to it.
public static object CopyUserControl(object obj, object obj2)
{
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
Type type2 = obj2.GetType();
PropertyInfo[] properties2 = type.GetProperties();
foreach (PropertyInfo property in properties)
{
foreach (PropertyInfo property2 in properties2)
{
// Match the loops and ensure it's not a non-custom property
if (property.Name == property2.Name && property.SetMethod != null && !property.SetMethod.IsSecuritySafeCritical)
property2.SetValue(obj2, property.GetValue(obj, null));
}
}
return obj2;
}
It's useful when you have to copy an ASCX control on the same page with a different html id. Usage:
// Copying myAscxControl1 onto myAscxControl2
myAscxControl2 = (ObjectNameToBeCasted)CopyUserControl(myAscxControl1, myAscxControl2);
It should work with different typed objects too. Property names that you want to copy must match though.
I have several different controls (TextBoxes, DateTimePickers, MaskedTextBoxes) on a form that I would like to check to see if they contain any data. I have the following code in the Click event of my "Save" button:
private void radBtnSave_Click(object sender, EventArgs e)
{
this.Cancelled = false;
bool bValid = true;
foreach(Control control in this.Controls)
{
if (control.Tag == "Required")
{
if (control.Text == "" || control.Text == null)
{
errorProvider.SetError(control, "* Required Field");
bValid = false;
}
else
{
errorProvider.SetError(control, "");
}
}
}
if (bValid == true)
{
bool bSaved = A133.SaveData();
if (bSaved != true)
{
MessageBox.Show("Error saving record");
}
else
{
MessageBox.Show("Data saved successfully!");
}
}
}
This works fine for the TextBoxes and MaskedEditBoxes, however, it does not work for the DateTimePickers. For those, I know I need to check the .Value property, but I cannot seem to access that from the Control object (i.e. "control.Value == "" || control.Value == null").
Am I missing something obvious? Any suggestions of modifications I can make to this code to allow me to check the DateTimePicker values (or just to improve the code at all) will be greatly appreciated.
You need to cast them to a DateTimePicker:
DateTimePicker dtp = control as DateTimePicker;
if(dtp !=null)
{
//here you can access dtp.Value;
}
Also, use String.IsNullOrEmpty(control.Text) in the first part of your code.
There is no Value property for Controls; DateTimePicker, for example, creates its own property that is unique to it.
Unfortunately for you, there is no fully generic way of handling this from a single loop of Control objects. The best you can do is something along the lines of this:
if(control is DateTimePicker)
{
var picker = control as DateTimePicker;
// handle DateTimePicker specific validation.
}
You'll need to do something like this:
foreach(Control control in this.Controls)
{
if (control.Tag == "Required")
{
DateTimePicker dtp = control as DateTimePicker;
if (dtp != null)
{
// use dtp properties.
}
else if (control.Text == "" || control.Text == null)
{
errorProvider.SetError(control, "* Required Field");
bValid = false;
}
else
{
errorProvider.SetError(control, "");
}
}
}