Dynamically Populate a CheckBoxList in C#





6
Date Submitted Sat. Oct. 22nd, 2005 5:19 PM
Revision 1 of 1
Helper ses5909
Tags CheckBoxList | CSharp | Populate | Values
Comments 2 comments
Dynamically Populate a CheckBoxList in C#
//The function to bind the database data to your CheckBoxList Control
protected void bindYourList()
{
        SqlConnection objConn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlServer"]);
        SqlCommand objCmd = new SqlCommand("select description, id from notificationLists", objConn);
        objConn.Open();
        cblList.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection);
        cblList.DataBind();
}

//In the Page_Load function call your behind method.
bindYourList();


//in your aspx page make sure to declare your CheckBoxList and define the DataTextField and DataValueField parameters.
<asp:checkboxlist id="cblList" runat="server" DataTextField="description" DataValueField="id">

Sara Smith

www.ilovecode.com
Sara

ilovecode

Comments

Comments Separation of Concerns?
Fri. May. 25th, 2007 2:34 PM    Beginner Jax
Comments Cleanup?
Sun. Oct. 8th, 2006 10:11 AM    Newbie Stylez

Voting