find controls that have been selected in asp.net - c#

Hi I'm building a web application for a client with some unique controls and criteria. One page involves a detailed search function consisting of about 100 combo boxes and listboxes. What I am trying to do is do a foreach(control that has been changed/selected){} and feed it into a function to generate a query. There are 100 but the user may only use three for a particular search ?
I know there must be a way to do this. Thanks in advance for the help

Well you can use something like:
List<Control> list = new List<Control>();
public void ControlRecursive(Control Root)
{
if(typeof(ComboBox) == Root.GetType() && ((ComboBox)Root).Checked)
list.Add(Root);
else if (Root is RadListBox)
{
// deal with RadListBox here
}
// if all your other types
// ...
foreach (Control Ctl in Root.Controls)
ControlRecursive(Ctl);
}
Then call this with ControlRecursive(form1);

You could have a javascript function associated with onchange / select for each of your controls and then add to a javascript array the item that has been selected. Then you can pass that array to your query generation to know what variables to pick up for your search.

Related

How to Find all Dropdown Lists inside of UpdatePanel on Child Page of a Master Page

I have an ASP.NET WebForms application that uses master pages and a single UpdatePanel. Inside this UpdatePanel, I have many other panels, which contain tables, which in turn, contain dropdown lists in the table cells.
I'd like to find all dropdown lists inside the panels and tables and check their SelectedValue for a validation method I'm working on. So far I have only been successful in finding the UpdatePanel and none of its child controls. I think I need to dig deeper into the respective sub-controls, but how to achieve this in a single method for all elements on the child page?
Master Page -> Child Page -> ContentPlaceHolder -> UpdatePanel -> Panel -> Table -> DropDownLists.SelectedValue
Have been trying many suggestions on SO, as well as Google, so far no dice. Any ideas?
In a nutshell, I'm looking to do something like this, but I think since my controls are nested in such a crazy way, the solution will end up being more complicated:
foreach(DropDownList ddl in d.Controls)
{
if (ddl.SelectedValue == "0")
HandleError(ddl.ID + " must have a value.");
}
Thanks for the reply #DMBeck.
I think I just found my answer, Good Ol' StackOverflow
This code from that answer seems to get me where I need to be:
IEnumerable<Control> EnumerateControlsRecursive(Control parent)
{
foreach (Control child in parent.Controls)
{
yield return child;
foreach (Control descendant in EnumerateControlsRecursive(child))
yield return descendant;
}
}
And then...
foreach (Control c in EnumerateControlsRecursive(Page))
{
if (c is DropDownList)
{
ControlList.Add(c);
}
}
foreach(DropDownList d in ControlList)
{
if(d.SelectedValue == "0")
{
HandleError(d.ID + " must have a value.");
}
}
Sounds like what you should be doing is client-side validation in conjunction with server-side validation in the on-click event in the code behind (assuming you are using older webform, not MVC).
For client-side validation I would suggest using JQuery Validation http://jqueryvalidation.org/.
On the server side just call the items by id, assuming the button is on the child page inside the update panel.

How to add check box inside combobox in c#

I want to add check box inside comboBox in C#. My purpose is that the user can select multiple values from one ComboBox ( Check all and Uncheck all ).
Please Help
You have to extend the ComboBox control by providing your own rendering strategy, and "manually" adding a CheckBox.
Theses open source project are ready to use :
http://www.codeproject.com/KB/combobox/CheckComboBox.aspx
http://www.codeproject.com/KB/combobox/extending_combobox.aspx
It is a wrong usage of a ComboBox control, because the user has no possibility to see his choices. For multiple selection, I recommend you to consider this CheckedListBox control:
link to MSDN
There is an ASP.NET open source control at http://dropdowncheckboxes.codeplex.com/ that I've used and been very happy with. There is also a WinForms open source control at http://www.codeproject.com/KB/combobox/extending_combobox.aspx that doesn't look quite as strong but maybe somebody could combine the best of both. If well implemented this is really a great addition to your toolkit. The above 2 implementations show all of the items selected and give you a number of related checkboxes in a reduced area and with excellent grouping. My addition to the ASP.NET version was to allow a list of checked files to use just file names instead of full paths if this gets too long. See above link for full code. Below is just my addition which is called instead of UpdateSelection in your postback handler:
// Update the caption assuming that the items are files
// If the caption is too long, eliminate paths from file names
public void UpdateSelectionFiles(int maxChars) {
StringBuilder full = new StringBuilder();
StringBuilder shorter = new StringBuilder();
foreach (ListItem item in Items) {
if (item.Selected) {
full.AppendFormat("{0}; ", item.Text);
shorter.AppendFormat("{0}; ", new FileInfo(item.Text).Name);
}
}
if (full.Length == 0) Texts.SelectBoxCaption = "Select...";
else if (full.Length <= maxChars) Texts.SelectBoxCaption = full.ToString();
else Texts.SelectBoxCaption = shorter.ToString();
}

Trying to figure out how to dynamically update attributes of ASP controls dynamically?

In my aspx page, I have a number of drop down controls.
I would like to update an attribute on each of the controls with "class=leftColumn" with the following line:
propertyID.Attributes["nameOfAttribute"] = "false";
But instead of manually writing out each controlID and setting its attribute with the line above, I had hoped there was a way to go through and set the attribute on each control ID if it had class=leftcolumn.
This is something I know is very easy with JQuery, BUT I need to do it with the code behind (C#)
I was told this is not possible (i.e. to acquire a list of all the controls and then iterate through the list and give it the attribute or any other way. That manually setting each control like the above example is the only way in ASP.NET.
Thanks,
Use LINQ for this. You can define an extension method to help. The ControlCollection needs some help. Something like:
foreach (DropDownList dd in this.Controls
.All()
.OfType<DropDownList>()
.Where(c => c.Attributes["foo"] == "bar"))
{
// do something
}
...
//define the extension method.
public static IEnumerable<Control> All(this ControlCollection controls)
{
foreach (Control control in controls)
{
foreach (Control grandChild in control.Controls.All())
yield return grandChild;
yield return control;
}
}
Kudos to David Findley at his ASP.NET blog.
You don't mention if the dropdowns are at least included in some common container to help finding them. If not, and you need to literally scan the entire page for any such dropdown, then I suggest creating a recursive method that starts at page level and enumerates the Controls collections of children and of children of children and so forth and analyzing each found control.

Checkboxlist + getting id's of all the checkboxes

How do I get the id's of all the checkboxes generated by a checkboxlist, with datatable as its data source?
I think I have to use the "OnDataBinding" event of the checkbox list, but I don't see how that will help me.
I am using C#
I don't think getting the id's of all the check boxes generated by the check box list is possible, so I think going the moo tools way is the right thing to do.
Any ideas?
Thanks
Ideally you would want to just attach the click event handlers to all your checkbox lists in the domReady event and this will create a much simpler function with MooTools. However, you can keep your code as-is if you prefer and just make your 2 functions a little simpler.
function ToggleSelection(ctrl, sender) {
var checkboxes = $(ctrl).getElements('input[type=checkbox]');
checkboxes.set('checked', sender.checked);
}
function ToggleSelectAll(ctrl, sender) {
var fAllChecked = ($(sender).getElements('input:checked').length == $(sender).getElements('input[type=checkbox]').length)
$(ctrl).set('checked', fAllChecked);
}
You can set the properties of your entire ELements array at once, you don't need to loop through them. In the 2nd function, I'm checking the number of elements that are checked against the total number of checkboxes and if they match that means they are all checked.
Are you using VB or C#..please tag accordingly
You can have List or string called strchklist
VB.NET
For Each li In CheckBoxList1.Items
If li.Selected Then
strchklist += li.Id
End If
Next
C#
foreach (ListItem li in CheckBoxList1.Items){
If li.Selected
strchklist += li.Id ;}
The <asp:ListItem> is not really a Control itself therefore has no ID. If you want to access it in client script add a new attribute you can reference. (yes, during OnDataBinding) Remember that these are not persisted in the ViewState however!
What exactly are you trying to accomplish? May help to elaborate somewhat.
Completely different answer...
The CheckBoxList is a bit of an odd duck... unlike other controls, it does not really have a logical mapping to an obvious HTML construct. It actually renders multiple checkboxes with derivative IDs. Those IDs seem to be generated as CheckBoxList.ClientID + "_" + ItemIndex.
You can verify this by looking at the page source. Internally, it seems the ID of the individual checkbox control is just its index, and then it's rendered with the CheckBoxList as its NamingContainer. You can use Reflector is see how the CheckBoxList control renders the output.
Still a good spot for jQuery. Just easier now you know the IDs.
I woke up this morning and thought of doing this (cblUSEquities is a checkbox list)
cblUSEquities.Attributes.Add("onclick", "javascript:alert('Clicked');");
This adds the alert to the table that the checkbox list generates. One thing to note is, the alert shows up twice If I click the text of the checkbox and once if I click the checkbox. I think this solution will work for me.
BTW, I never thought that the above code would work...
P.S Answering my own question because I wanted to write some code, which I cannot do using the comment box.
In the spirit of StackOverFlow, I arrived to something which works in my scenario but the description of the question is different? What do I do? Edit the question and mark this as answer?
I was able to do this by using mootools and this is the code.
function ToggleSelection(ctrl, sender)
{
var cblCtrl = $(ctrl);
var Allcbs = cblCtrl.getElements('input');
for(var i=0; i<Allcbs.length; i++)
Allcbs[i].checked = sender.checked;
}
function ToggleSelectAll(ctrl, sender)
{
var AllTrueCount = 0;
var cblCtrl = $(ctrl);
var Allcbs = sender.getElements('input');
for(var i=0; i<Allcbs.length; i++)
if(Allcbs[i].checked)
AllTrueCount++;
if(AllTrueCount == Allcbs.length)
cblCtrl.checked = true;
else
cblCtrl.checked = false;
}
C# code which calls the javascript functions
//Binding event to the checkbox list
cblUSEquities.Attributes.Add("onclick", string.Format("javascript:ToggleSelectAll('{0}', this);", chkAllUSEquities.ClientID));
//binding event to the select all checkbox
chkAllUSEquities.Attributes.Add("onclick", string.Format("javascript:ToggleSelection('{0}', this);", cblUSEquities.ClientID));
As it turns out I did not need to know the id's of all the checkboxes generated by the checkbox list. I was able to add the onclick javascript to those checkboxes by this line
cblUSEquities.Attributes.Add("onclick", string.Format("javascript:ToggleSelectAll('{0}', this);", chkAllUSEquities.ClientID));
Which will add the onclick event to the table that the checkbox list generates.

getting all select inputs on a single page in the C# code behind file of ASP.NET

How would i go about getting all select inputs on a single page in the C# code behind file of ASP.NET ?
these select inputs are a part of a control, and the page contains several of the controls.
Assuming that these controls are runat="server", you want to use the Controls collection on the page and, recursively, any container controls (HasControls is true for that control) that you find from the page. Add any found control that matches the type you are interested in (DropDownList or HtmlSelect) to your list. If the selects are not runat="server", then I think you're out of luck unless their naming gives them away. You can't tell the type of an HTML input from the contents of the Form collection.
public List<Control> FindSelects( Control control, List<Control> controls )
{
if (control is DropDownList or control is HtmlSelect)
{
controls.Add( control );
}
else if (control.HasControls)
{
foreach (var subcontrol in control.Controls)
{
controls = FindSelects( subcontrol, controls );
}
}
return controls;
}

Categories

Resources