Solved

CheckBoxList setting value

Posted on 2011-03-09
2
585 Views
Last Modified: 2012-05-11
Hi EE,
I have CheckBoxList on aspx page and adding items from code behind page.
in the following code, how can I set "ID" for value and "Text" for item text to display.

[.aspx]
 <asp:CheckBoxList ID="SectorList" runat="server">
 </asp:CheckBoxList>

[.cs]
while (reader.Read())
 {
string ID = reader["ID"].ToString();
string Text = reader["Text"].ToString();
SectorList.Items.Add(Text);
}


eg.
   <asp:CheckBoxList ID="SectorList" runat="server">
                    <asp:ListItem Value="1">apple</asp:ListItem>
                    <asp:ListItem Value="2">orange</asp:ListItem>
   </asp:CheckBoxList>
0
Comment
Question by:saloj
2 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 400 total points
ID: 35089639
SectorList.Items.Add(new ListItem(Text, ID));
0
 
LVL 10

Assisted Solution

by:hosneylk
hosneylk earned 100 total points
ID: 35089902
you can also do it this way instead of looping.
SectorList.DataValueField = "ID";
SectorList.DataTextField = "Text";
SectorList.DataSource = reader;
SectorList.DataBind();

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now