item.selected Not working in ListBox - asp.net

I have this piece of code:
foreach (ListItem item in lbUnassigned.Items)
{
if (item.Selected)
{
string itemName = item.Text.ToString();
string itemValue = item.Value.ToString();
lbAssigned.Items.Add(new ListItem(itemName, itemValue));
lbUnassigned.Items.Remove(lbUnassigned.SelectedItem);
}
}
Which is pretty much identical to an example given in the .NET 3.5 book I have, yet when stepping through this procedure the item.selected if false every time, even though I am selecting at least one value in the ListBox.
Any ideas what I could be doing wrong?

Usually when something like this happens the reason is control rebinding. Check if you have the if(!IsPostBack) on your page load when binding the control

Related

ASP:ListBox Get All Values As Selected Items

I'm just wondering how can I possibly get all values in a ListBox as selectedItems? Note that the viewed records came from an SQL datasource. I'll be using these values as list of email addresses for an email blast.
Any help would be much appreciated, thanks.
I'm using ASP.Net C#
if you want to select all items in a listbox control, Use This
listView1.MultiSelect = true;
foreach (ListItem item in ListBox1.Items )
{
item.Selected = true;
}
or you can do like this with LINQ :
Just pass your listview and checkstate to the function.
public void CheckAllItems(ListView lvw, bool check)
{
lvw.Items.OfType<ListViewItem>().ToList().ForEach(item => item.Checked = check);
}

Bind multiple dropdownlists to same datasource using loop

I have a form with 15 dropdownlists inside a table. I would like populate each ddl with the same values from a single datasource.
Instead of doing the below x 15:
ddl.datasource = x
ddl.databind()
Is it possible to loop through all the DDL's in a table and assign the datasource etc all in one go?
Something like this? (I know the code is wrong but I am unsure of the full correct code)
For Each ctrl In tblNetwork.Controls
If TypeOf ctrl Is DropDownList Then
ddl.DataSource = usr
ddl.DataBind()
End If
Next ctrl
Seems a basic one but it's got me stumped.
You are getting stumped because your method is only going to traverse the top-level controls since iterating over tblNetworks.Controls won't be recursive.
I think that this is, in general, a very bad idea because your page will suffer from the recursion that you need to make in order to catch all the dropdowns in the page. With that said, here's a solution (in C#, sorry) that will work, regardless of the level of nesting of your dropdown lists.
First, you need to create a helper method that flattens any IEnumerable so we can use LINQ:
public static class ControlExtensions
{
public static IEnumerable<T> Flatten<T>( this IEnumerable<T> e, Func<T, IEnumerable<T>> f)
{
return e.SelectMany(c => f(c).Flatten(f)).Concat(e);
}
}
And now your code becomes this:
var result = this.Controls.Cast<DropDownList>().Flatten(x=>x.Controls.Cast<DropDownList>());
DropDownList current;
foreach (var item in result)
{
if (item is DropDownList)
{
current = item as DropDownList;
current.DataSource = your_data_source;
current.DataBind();
}
}
You can obviously extend this further to do the same on any page you need to, but the more complicated your page is, the bigger your performance hit.
I hope I persuaded you from doing this...

Nested Foreach Loop not working?

I have 2 datagrids. Both with checkboxes. I have a nested foreach loop that gets the DataKeys values of both checked persons from both datagrids. When I run the debugger the first datakey gets assigned to the correct variable oIdividualID. That is working fine. The second foreach loop gets the datakey from the next datagrid. That works fine too. However, it is not assigning it to the variable oNewParentID. and At the bottom of my code, in debugging, the method I am calling holds the value of oIndividualID correctly, But oNewParentID Holds NO value? Is there something wrong with my nested foreach loop? I don't understand. Here's my code:
protected void imgbtnReassgin_Click(object sender, ImageClickEventArgs e)
{
foreach (GridViewRow row in gvAdminCustomer.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkitemSelectorCustomers");
if (cb != null && cb.Checked)
{
int oIndividualID = Convert.ToInt32((gvAdminCustomer.DataKeys[row.RowIndex].Value));
foreach (GridViewRow r in gvReassignCustomers.Rows)
{
CheckBox chkBox = (CheckBox)row.FindControl("chkitemSelectorAllManagersandSalesman");
if (chkBox != null && chkBox.Checked)
{
int oNewParentID = Convert.ToInt32((gvReassignCustomers.DataKeys[r.RowIndex].Value));
Individual ind = new Individual();
ind.ReassignIndividual(oIndividualID, oNewParentID);
}
}
gvReassignCustomers.DataBind();
gvAdminCustomer.DataBind();
}
}
}
hard to understand the purpose of the code - for every checked tickbox in Admin grid you're running reassign on every checked Reassign grid. Is it really the intent?
And then you're running DataBind() for every line on in admin grid.
So.
Comment out DataBind() - you don't need them there, it's just tells the system to rebind the data to the grids which can potentially clear the user ticked tickboxes.
Not quite sure what happened, but suddenly it all started working. Problem solved but not sure how.

ASP Multiselect listbox separator

I have encountered a problem and I didn't manage to find any soultions yet. Let me simplify things a bit.
I have 2 forms, the first contains an ASP ListBox with multi select mode enabled. I submit the form and in the other form I use just for testing purposes this snippet of code:
protected void Page_Load(object sender, EventArgs e)
{
foreach (string formKey in Request.Form.AllKeys)
{
if (formKey != null)
{
if (formKey.Equals("ctl00$MainContent$ListBox1"))
Label1.Text = Request.Form[formKey];
}
}
}
The problems is that the values that come from the listbox (the values that i selected in the previous form) are separated by "," for ex. "test1,test2,test3". How can i change this separator to "$" for example? I need to change it because the actual values may contain "," and i don't manualy feed them to the listbox.
I can't use any other mode of transfering this values between the form because the entire application uses this model. The values that i get are then sent to a workflow where there will be manipulated and in the workflow i need to know where each listbox item starts and ends so it must be an unique separator.
Any help is apreciated! Thank you very much
Thank you MatteKarla but unfortunately this does not solve my problem. Yes, this is a good way of transfering the values from one form to another.
However i must use the method I described above with Request form keys because the listbox is one of many others "parameters" that are generated at runtime and have their values sent to a workflow method that takes this values. And i can't afford to change that in my application.
My problem is that coma (",") separator is used by default with a multiselect listbox.
I thought that there maybe is a method to change that separator from coma to another char because the coma can also be included in the value itself and this will create confusion.
As i said if i select three values test1, test2 and test3, the result with my method will be a string looking like "test1,test2,test3". However a "test1$test2$test3" would be much better.
But I'm affraid that changing this default separator is not possbile. I must think at a method to overcome this problem like replacing before feeding the listbox all the intended coma from the values with some other char not to create confusion. But this is not a great way of doing it.
On your first page/form (First.aspx.cs) create a public property with the listbox:
public ListBox PostedListBox { get { return ListBox1; } }
Set the postback-url for the button to Second.aspx
Second page in the aspx-file after the #Page-directive add:
<%# PreviousPageType VirtualPath="~/First.aspx" %>
Then in Form_Load on Second.aspx.cs you can extract the values:
if (PreviousPage != null)
{
ListBox postedListbox = PreviousPage.PostedListBox;
foreach (var index in postedListbox.GetSelectedIndices())
{
var itemText = postedListbox.Items[index].Text;
}
}
Or you could just try to locate the control by using:
if (PreviousPage != null)
{
var control = PreviousPage.FindControl("ListBox1") as ListBox;
}
Third Edit:
You could use GetValues:
Request.Form.GetValues("ctl00$MainContent$ListBox1");
returns a string array containing each of the selected items.

Accessing asp.net tablerow child control by type

I'm iterating through a collection of asp:tablerows to be able to get or set the text in a textbox that is nested in the third cell of the row; I'm doing this by type rather than by ID because the cell ID's in that column aren't totally consistent--thus I can't really call FindControl() to achieve this. I've resorted to casting the third control in the TableRow to a TableCell and then Casting the first control in that cell to a TextBox. Not quite correct, as I'm getting an index out of range exception thrown. The problem mainly lies in the Controls.Count() property of the third cell, which comes to zero.
Not sure if there's a better way to access the textbox---should I resort to FindControl()?
The code:
foreach (TableRow row in tblProviders.Rows) {
string value = ((TextBox)((TableCell)row.Controls(2)).Controls(0)).Text;
...
}
My searches here only yielded use of FindControl(), so that may be the only way...
Thanks!
You could use Linq as follows:
var TextBoxes = tblProviders.Rows.OfType<TableRow>()
.SelectMany(row => row.Cells.OfType<TableCell>()
.SelectMany(cell => cell.Controls.OfType<TextBox>()));
TextBoxes would then be a collection of all the textboxes in tblProviders.Rows, which you could then itterate through and do what you like with.
A little bit of null checking wouldn't go amiss here.
You could try using this Recursive call:
foreach (TableRow row in tblProviders.Rows) {
var tb = FindControlRecursive(row, typeof(TextBox));
if (tb != null) {
string value = ((TextBox)tb).Text;
}
}
private Control FindControlRecursive(Control rootControl, Type controlType) {
if (rootControl.GetType() == controlType)
return rootControl; //Found it
foreach (Control controlToSearch in rootControl.Controls) {
Control controlToReturn = FindControlRecursive(controlToSearch, controlType);
if (controlToReturn != null) return controlToReturn;
}
return null;
}

Resources