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
Related
On my web application I am using a checkbox list .I am binding this Checkbox list using a datatable like
cblMenuAccess.DataTextField = "Role_Name";
cblMenuAccess.DataValueField = "Role_Code";
cblMenuAccess.DataBind();
and now I am trying to make some checkbox checked from C# code.Its not working as expected.
This is i did so far
foreach (DataRow row1 in dt2.Rows)
{
string Role_Code = row1["User_Role_Code"].ToString();
foreach (ListItem oItem in cblMenuAccess.Items)
{
if (oItem.Value == Role_Code)
{
//oItem.Value need to be checked true
}
}
}
Do anyone know how to make this checkbox checked with respect to its value ??
i give you simple example of checkboxlist
Select Fruit:
<asp:CheckBoxList ID="chkFruits" runat="server">
<asp:ListItem Text="Apple" Value="1" />
<asp:ListItem Text="Mango" Value="2" />
<asp:ListItem Text="Papaya" Value="3" />
<asp:ListItem Text="Banana" Value="4" />
<asp:ListItem Text="Orange" Value="5" />
</asp:CheckBoxList>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick = "Submit" />
protected void Submit(object sender, EventArgs e)
{
string message = "";
foreach (ListItem item in chkFruits.Items)
{
if (item.Selected)
{
message += "Value: " + item.Value;
message += " Text: " + item.Text;
message += "\\n";
}
}
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}
I got several checkbox, but I only want the the text "New Desktop" and "New Laptop" can only choose 1 out of 2. I hope it could be done in C#.
<asp:CheckBoxList ID="Service1" runat="server"
Width="251px" >
<%--onselectedindexchanged="checkBox1_CheckedChanged"--%>
<asp:ListItem text="New Login ID & Email Address" ></asp:ListItem>
<asp:ListItem text="New Desktop" Value="2" oncheckedchanged="checkBox1_CheckedChanged" ></asp:ListItem>
<asp:ListItem text="New Notebook" Value="3" oncheckedchanged="checkBox2_CheckedChanged"></asp:ListItem>
<asp:ListItem text="New Mouse"></asp:ListItem>
<asp:ListItem text="New Keyboard"></asp:ListItem>
<asp:ListItem text="New Printer"></asp:ListItem>
</asp:CheckBoxList>
//.cs pages
protected void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (Service1.Items[2].Selected == true)
{
Service1.Items[3].Enabled = false;
}
}
protected void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (Service1.Items[3].Selected == true)
{
Service1.Items[2].Enabled = false;
}
}
I am giving you one example below but still this may not be a feasible way. You need to add CheckedChanged event to each CheckBox to remove check from other CheckBoxes
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if(CheckBox1.Checked)
{
CheckBox2.Checked =false;
CheckBox3.Checked =false;
CheckBox4.Checked =false;
}
}
You should repeat the above evevt for CheckBox2 , CheckBox3 and so on. You can extend it as per your requirement.
But in this scenario I would recommend you to use the ASP.NET Radio Button Control. Please refer this link for more information on Radio Button Control.
You can use OnSelectedIndexChanged event for CheckBoxList with AutoPostBack="True" so your control send request to server and selection chenged event will be call
I.e : For design
<asp:CheckBoxList ID="Service1" runat="server" Width="251px" AutoPostBack="True" OnSelectedIndexChanged="Service1_SelectedIndexChanged">
<asp:ListItem Text="New Login ID & Email Address"></asp:ListItem>
<asp:ListItem Text="New Desktop" Value="2"></asp:ListItem>
<asp:ListItem Text="New Notebook" Value="3" ></asp:ListItem>
<asp:ListItem Text="New Mouse"></asp:ListItem>
<asp:ListItem Text="New Keyboard"></asp:ListItem>
<asp:ListItem Text="New Printer"></asp:ListItem>
</asp:CheckBoxList>
and in code: 'li' have all selected item and as per your requirement "New Desktop" and "New Laptop" only choose 1 out of 2 .
protected void Service1_SelectedIndexChanged(object sender, EventArgs e)
{
CheckBoxList li = (CheckBoxList)sender;
foreach (ListItem l in li.Items)
{
if (l.Value == "2")
{
if (l.Selected)
{
Service1.Items[2].Enabled = false;
}
else
{
Service1.Items[2].Enabled = true;
}
}
else if (l.Value == "3")
{
if (l.Selected)
{
Service1.Items[1].Enabled = false;
}
else
{
Service1.Items[1].Enabled = true;
}
}
}
}
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;
}
}
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
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;