Solved

CheckBoxList setting value

Posted on 2011-03-09
2
588 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Please explain "Multi-Tenant Services" 5 63
Error on Add method 1 38
Replace &lt; with < 14 56
Help with C#, MVC, razor. 6 23
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 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

23 Experts available now in Live!

Get 1:1 Help Now