Problem in selecting multiple items in Listbox.SelectionMode - c#

I have a webpart and I want to select mulitple items in a listbox,pretty easy.I am using webcontrols namespace.So I am declaring listbox as
ListBox lBox = new ListBox();
lBox.ID="lbox";
lBox.SelectionMode="Multiple";
But it is not accepting that . The error I m getting is cannot convert string type to listbox selection sth..
If anyone is having any idea where I m getting wrong?
Thanks,

From Programmatically Select Multiple Items
<div>
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Value="One" />
<asp:ListItem Value="Two" />
<asp:ListItem Value="Three" />
<asp:ListItem Value="Four" />
<asp:ListItem Value="Five" />
</asp:ListBox></div>
</div>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
ListBox1.SelectionMode = System.Web.UI.WebControls.ListSelectionMode.Multiple;
for (int i = 0; i < ListBox1.Items.Count; i++)
{
// Select the first, third and fifth items in the listbox
if(i == 0 || i == 2 || i == 4)
{
ListBox1.Items[i].Selected = true;
}
}
}

Try:
lBox.SelectionMode = ListSelectionMode.Multiple;

Try this:
ListBox l = new ListBox();
l.SelectionMode = ListSelectionMode.Multiple;

Related

Disable and checked values of `CheckBoxList` memorized in database in c#

I have added in an form in aspx page this CheckBoxList:
<asp:CheckBoxList ID="Fruits" runat="server">
<asp:ListItem Text="Mango" Value="1" />
<asp:ListItem Text="Apple" Value="2" />
<asp:ListItem Text="Banana" Value="3" />
<asp:ListItem Text="Guava" Value="4" />
<asp:ListItem Text="Orange" Value="5" />
</asp:CheckBoxList>
In this CheckBoxList it's possible select multiple values, e.g. :
And this values are memorized in field of database table:
for (int i = 0; i < Fruits.Items.Count; i++)
{
if (Fruits.Items[i].Selected == true)
{
FruitsUpdate += Fruits.Items[i].Text.ToString() + ";";
}
}
Value memorized in database : 1;4;5;
Now in aspx form page I need disable and checked the values of CheckBoxList memorized in database, but I have tried this code without success because all ListItem are disabled but not checked.
FruitsDB = dr["Fruits"].ToString();
if (FruitsDB.ToString() != "")
{
Fruits.Text = FruitsDB.ToString();
Fruits.Enabled = false;
}
else
{
Fruits.Enabled = true;
}
I need disable and checked in the CheckBoxList the items with value 1 and 4 and 5.
Please help me, thank you in advance.
You didn't set the Selected property of the list item where you set the Enabled property.
Here is the code. You will iterate over CheckBoxList. if current checkbox's value is in FruitsDB then it will be selected and disabled.
var memory = FruitsDB.ToString();
foreach (ListItem item in Fruits.Items)
{
if (memory.Contains(item.Value))
{
item.Enabled = false;
item.Selected = true;
}
}

Find a CheckBoxList item by its value within many CheckBoxLists - asp.net

I have many CheckBoxList in my page. Is it possible to search for individual ListItems by their value from the Code Behind?
<div class="col-lg-2">
<h4>Ski</h4>
<asp:CheckBoxList ID="ski" runat="server">
<asp:ListItem Text="Hauts" Value="43" runat="server"></asp:ListItem>
<asp:ListItem Text="Bas" Value="45" runat="server"></asp:ListItem>
<asp:ListItem Text="Sous-vĂȘtements" Value="89" runat="server"></asp:ListItem>
</asp:CheckBoxList>
</div>
<div class="col-lg-2">
<h4>Ski de fonds</h4>
<asp:CheckBoxList ID="skidefonds" runat="server">
<asp:ListItem Text="Hauts" Value="42" runat="server"></asp:ListItem>
<asp:ListItem Text="Bas" Value="46" runat="server"></asp:ListItem>
<asp:ListItem Text="Sous-vĂȘtements" Value="90" runat="server"></asp:ListItem>
</asp:CheckBoxList>
</div>
EDIT:
To add more precision on the question: How to set the ListItem which as a value of 46 to Selected, without knowing which CheckboxList it belongs.
Thanks
Yes, it is possible. Based on your structure, you would simply do:
// Selected:
IList<ListItem> selected = skidefonds.Items.Cast<ListItem>()
.Where(i => i.Selected).ToList();
You can also expose the content through a loop.
foreach(var item in skidefonds.Items)
{
// Item will have all the attributes.
}
You can expose the value by doing the following:
var result = skidefonds.Items.FindByValue("...");
Or you can access the attribute directly on your item as you iterate through the loop.
foreach(var item in skidefonds.Items)
item.FindByValue("...");
Use a recursive method like this:
protected void Page_Load(object sender, EventArgs e)
{
FindValue(this);
}
private void FindValue(Control ctrls)
{
foreach (Control c in ctrls.Controls)
{
if (c is CheckBoxList)
{
CheckBoxList ff = c as CheckBoxList;
for (int i = 0; i < ff.Items.Count; i++)
{
if ((string)ff.Items[i].Value == "46")
{
ff.Items[i].Selected = true;
}
}
}
else FindValue(c);
}
}
You can loop all checkbox lists and then within that loop all list items looking for the one you want.
foreach(Control cbxList in this.Controls)
{
if(cbxList is CheckBox)
{
for (int i = 0; i < cbxList.Items.Count; i++)
{
if ((string)cbxList.Items[i] == value)
{
cbxList.SetItemChecked(i, true);
}
}
}
}
List<ListItem> skiValues = ski.Items.Cast<ListItem>()
.Where(li => li == 'Search')
.ToList();
try this
var item=ski.Items.FindByValue("yourvalue");

Store a Selected Items From a Listbox

I can't seem to get my application to store the selected items from a listbox.
Each time I run the code and debug it selects the first item in the list as the selected item from the listbox even if it is not selected and stores this item but for the actual items that have been selected they are ignored. This even happens if I set all properties of the listitems to Selected="false" in the ASP tag.
I have the same code used for a checkboxlist and it works fine I cannot see why this is not working?
I have not included the Answers class as it just gets and sets the properties.
<asp:ListBox runat="server" ID="lbQuestion3" SelectionMode="Multiple" Rows="7">
<asp:ListItem Value="0" >Dublin</asp:ListItem>
<asp:ListItem Value="1" >Cork</asp:ListItem>
<asp:ListItem Value="1" >Waterford</asp:ListItem>
<asp:ListItem Value="1" >Limerick</asp:ListItem>
<asp:ListItem Value="0" >Carlow</asp:ListItem>
<asp:ListItem Value="0" >Galway</asp:ListItem>
<asp:ListItem Value="0" >Kilkenny</asp:ListItem>
</asp:ListBox>
<br />
<asp:Button ID="btnQuestion4" runat="server" Text="Next Question" OnClick="btnQuestion4_Click"/>
C# code
protected void btnQuestion4_Click(object sender, EventArgs e)
{
Answers question3;
List<Answers> answers = new List<Answers>();
foreach (ListItem item in lbQuestion3.Items)
{
if (item.Selected == true)
{
question3 = new Answers(item.Text, Convert.ToInt32(item.Value), 0);
answers.Add(question3);
}
Session["ssAnswerQ3"] = answers;
Response.Redirect("Question4.aspx");
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["ssAnswerQ3"] != null)
{
List<Answers> previousAnswers = new List<Answers>();
previousAnswers = (List<Answers>)Session["ssAnswerQ3"];
foreach (ListItem item in lbQuestion3.Items)
{
foreach (var item2 in previousAnswers)
{
if (item2.Answer == item.Text)
{
item.Selected = true;
}
}
}
}
}
}
Any help would be greatly appreciated.
John

how to delete from selected items of listbox in asp.net

I have a listbox which contain 6 items . In the seleceted index changed event of list box text of label is changed to the selected item value.Select mode of listbox is multiple.I want
When a user select an item it should remove from listbox and add to label text.
user can remove the selected items 1 2 or all
I am not using textbox because i want only listbox items can be select.
my html is
<asp:Label ID="lbl_mar_cat" runat="server" Width="100%" Font-Size="Small"></asp:Label>
<asp:ListBox ID="listbox_mar" runat="server" SelectionMode="Multiple"
CssClass="listbox" AutoPostBack="True"
onselectedindexchanged="BulletedList1_SelectedIndexChanged" >
<asp:ListItem Value="Doesn't Matter">Doesn't Matter</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>Divorced</asp:ListItem>
</asp:ListBox>
my server side code is
if (listbox_mar.SelectedValue == "Doesn't Matter")
{
lbl_mar_cat.Text = "Doesn't Matter";
}
else
{
if (lbl_mar_cat.Text == "")
{
lbl_mar_cat.Text = listbox_mar.SelectedValue.ToString();
}
else
{
lbl_mar_cat.Text += ", " + listbox_mar.SelectedValue.ToString();
}
}
Try this:
protected void listbox_mar_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < listbox_mar.Items.Count; i++)
{
if (listbox_mar.Items[i].Selected)
{
lbl_mar_cat.Text += listbox_mar.Items[i].Text+ " , " ;
listbox_mar.Items.Remove(listbox_mar.Items[i]);
}
}
}

how to add an OnClick event on DropDownList's ListItem that is added dynamically?

Say I have the following code:
DropDownList changesList = new DropDownList();
ListItem item;
item = new ListItem();
item.Text = "go to google.com";
changesList.Items.Add(item);
How do you dynamically add an OnClick event to item so that google.com will be visited after clicking on the item?
Add this to your code:
DropDownList changesList = new DropDownList();
ListItem item;
item = new ListItem();
item.Text = "go to google.com";
item.Value = "http://www.google.com";
changesList.Items.Add(item);
changesList.Attributes.Add("onChange", "location.href = this.options[this.selectedIndex].value;");
First declare the dropdownlist
<asp:DropDownList ID="ddlDestination" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
<asp:ListItem Text="Select Destination" Selected="True" />
<asp:ListItem Text="Go To Google.com" Value="http://www.google.com" />
<asp:ListItem Text="Go To Yahoo.com" Value="http://www.yahoo.com" />
<asp:ListItem Text="Go To stackoverflow.com" Value="http://www.stackoverflow.com" />
</asp:DropDownList>
Second, in the code behind put this code
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlDestination.SelectedIndex > 0)
{
string goToWebsite = ddlDestination.SelectedValue;
Response.Redirect(goToWebsite);
}
}
Hope this helps

Categories

Resources