Trying to find controls - c#

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.

Related

How can I get all TextBoxes inside of a Custom UserControl?

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
}
}
}

How to clear form data

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

get all control from multiple form in csharp

I want to get all the control from multiple form (Main, Two and Three) and
compare if the control tag equals the variable str_name and if true write the
value of str_value in c.Text.
the code:
private static Form[] getformular()
{
Main main = new Main();
Two f2 = new Two();
Three f3 = new Three();
Form[] form = { main, f2, f3};
return form;
}
private void initcontrol()
{
String str_name = "name";
String str_value = "value";
foreach(Form f in getformular())
{
foreach (Control c in f.Controls)
{
if (f != null && c = null)
{
if (c.Tag.Equals(str_name))
{
c.Text = str_value;
}
}
}
}
}
Could please someone help me?
First, as stated by #JonB some of conditional checking (ifs logic) in your current code seems off.
Second, looping through Form.Controls will only bring you all controls placed directly in the Form. For example if you have tab control (or any other container control) placed in form, and you have a textbox inside that tab control, you'll get only the tab control and couldn't find the textbox by looping through Form.Controls. You can solve that with recursive method as demonstrated below.
private void initcontrol()
{
String str_name = "name";
String str_value = "value";
var result = false;
foreach(Form f in getformular())
{
//if you want to check if f null, it should be here.
if(f != null) result = setControlText(f, str_name, str_value);
}
if(!result) MessageBox.Show("Control not found");
}
private bool setControlText(Control control, string str_name, string str_value)
{
var isSuccess = false;
foreach (Control c in control.Controls)
{
//if c is not null
if (c != null)
{
//check c's Tag, if not null and matched str_name set the text
if (c.Tag != null && c.Tag.Equals(str_name))
{
c.Text = str_value;
isSuccess = true;
}
//else, search in c.Controls
else isSuccess = setControlText(c, str_name, str_value);
//if control already found and set, exit the method now
if (isSuccess) return true;
}
}
return isSuccess;
}

Looping through children controls inside Updatepanel

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

How can I loop thru all controls (including ToolStripItems) C#

I need to save and restore settings for specific controls on a form.
I loop thru all controls and return the one whose name matches the one I want, like so:
private static Control GetControlByName(string name, Control.ControlCollection Controls)
{
Control thisControl = null;
foreach (Control c in Controls)
{
if (c.Name == name)
{
thisControl = c;
break;
}
if (c.Controls.Count > 0)
{
thisControl = GetControlByName(name, c.Controls);
if (thisControl != null)
{
break;
}
}
}
return thisControl;
}
From this I can determine the type of control and therefore the property that should be / has been stored.
This works well unless the control is one of the ToolStrip family which has been added to a toolstrip. e.g.
this.toolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.lblUsername, // ToolStripLabel
this.toolStripSeparator1,
this.cbxCompany}); // ToolStripComboBox
In this case I can see the control I'm interested in (cbxCompany) when debugging, but the name property has no value so the code does not match to it.
Any suggestions on how I can get to these controls too?
Cheers,
Murray
Thanks for your help guys.
Pinichi set me on the right track, I was checking toolStrip.Controls - should have been toolStrip.Items
The code below now works perfectly for me:
private static Control GetControlByName(string controlName, Control.ControlCollection parent)
{
Control c = null;
foreach (Control ctrl in parent)
{
if (ctrl.Name.Equals(controlName))
{
c = ctrl;
return c;
}
if (ctrl.GetType() == typeof(ToolStrip))
{
foreach (ToolStripItem item in ((ToolStrip)ctrl).Items)
{
if (item.Name.Equals(controlName))
{
switch (item.GetType().Name)
{
case "ToolStripComboBox":
c = ((ToolStripComboBox)item).Control;
break;
case "ToolStripTextBox":
c = ((ToolStripTextBox)item).Control;
break;
}
if (c != null)
{
break;
}
}
}
}
if (c == null)
c = GetControlByName(controlName, ctrl.Controls);
else
break;
}
return c;
}
Try This:
//for toolstrip
if (ctrl is ToolStrip)
{
ToolStrip ts = ctrl as ToolStrip;
foreach (ToolStripItem it in ts.Items)
{
if (it is ToolStrienter code herepSeparator)
{
//-------------------------
}
else
{
//do something
}
}
}//---------------

Categories