I am having a checked list box from which i extracted the checked items value and saved it to database. Now when i am returning back to form i am having only the value [id] as a List. Now i want to display those selected values from this list of int.
I think i am clear to you.
[Update]
Hi, I solved my issue , using the below code. But i want to know is there any more optimized solution than this one. As its itera开发者_如何学运维ting unneccesarily
private void ProcessRequest()
{
if (Session["DeniedReason"] != null)
{
List<int> deniedList = (List<int>)Session["DeniedReason"];
if(deniedList!=null)
{
if(deniedList.Count>0)
{
foreach (int deniedValue in deniedList)
{
foreach (ListItem item in cblDeniedList.Items)
{
if (string.Compare(item.Value,deniedValue.ToString())==0)
{
item.Selected = true;
}
}
}
}
}
}
}
LINQ / Lambda expression if any..that would be great.
List<int> deniedList = (List<int>)Session["DeniedReason"];
foreach (ListItem li in cblDeniedList.Items)
{
li.Selected = deniedList.Contains(Convert.ToInt32(li.Value));
}
<asp:checkboxlist id="CheckBoxList1" runat="server"
DataTextField="interest" DataValueField="1" />
Use DatavalueFild property(i think it's your id)
ListItem myListItem = CheckBoxList1.Items.FindByValue("sports");
currentCheckBox.Selected = true;
or use CheckBoxList1.Items.FindByText method Hope this helps:-) It's answer for you nonupdated question
精彩评论