ASP.NET Tutorial/Data Binding/CheckBoxList
CheckBoxList Control data binding with asp:SqlDataSource
<source lang="csharp">
Three properties that affect its layout: RepeatColumns: The number of columns of check boxes to display. RepeatDirection: The direction in which the check boxes are rendered.
Possible values are Horizontal and Vertical.
RepeatLayout: Determines whether the check boxes are displayed in an HTML table.
Possible values are Table and Flow.
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">
protected void btnSubmit_Click(object sender, EventArgs e) { foreach (ListItem item in cblProducts.Items) if (item.Selected)lblProduct.Text += "
<asp:CheckBoxList id="cblProducts" DataSourceID="srcProducts" DataTextField="Title" DataValueField="Id" RepeatColumns="2" Runat="server" /> <asp:Button id="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" Runat="server" />
<asp:Label id="lblProduct" EnableViewState="false" Runat="server" /> <asp:SqlDataSource id="srcProducts" SelectCommand="SELECT Id, Title FROM Products" ConnectionString="<%$ ConnectionStrings:Products %>" Runat="server" />
</form>
</body> </html>
File: Web.config
<configuration>
<connectionStrings> <add name="Products" connectionString="Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" /> </connectionStrings></configuration></source>