[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

CheckBoxList setting value

Posted on 2011-03-09
2
Medium Priority
?
601 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 1600 total points
ID: 35089639
SectorList.Items.Add(new ListItem(Text, ID));
0
 
LVL 10

Assisted Solution

by:hosneylk
hosneylk earned 400 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

872 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