CheckBoxList控件小用,SqlDataSource数据源
Code
<!-- You will need SQL Server 2000/2005 and a connection
to the Customers table in the Northwind database in the web.config for this example to work -->
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "You selected:<br>";
foreach (ListItem li in CheckBoxList1.Items) {
if (li.Selected == true) {
Label1.Text += li.Text + "<br>";
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>CheckBox control</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" Runat="server" Text="Submit Choices"
OnClick="Button1_Click" />
<br />
<br />
<asp:Label ID="Label1" Runat="server"></asp:Label>
<br />
<asp:CheckBoxList ID="CheckBoxList1" Runat="server"
DataSourceID="SqlDataSource1" DataTextField="name"
RepeatColumns="3" BorderColor="Black"
BorderStyle="Solid" BorderWidth="1px">
</asp:CheckBoxList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SqlConnectionString1 %>" SelectCommand="SELECT name FROM [students]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
<!-- You will need SQL Server 2000/2005 and a connection
to the Customers table in the Northwind database in the web.config for this example to work -->
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "You selected:<br>";
foreach (ListItem li in CheckBoxList1.Items) {
if (li.Selected == true) {
Label1.Text += li.Text + "<br>";
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>CheckBox control</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" Runat="server" Text="Submit Choices"
OnClick="Button1_Click" />
<br />
<br />
<asp:Label ID="Label1" Runat="server"></asp:Label>
<br />
<asp:CheckBoxList ID="CheckBoxList1" Runat="server"
DataSourceID="SqlDataSource1" DataTextField="name"
RepeatColumns="3" BorderColor="Black"
BorderStyle="Solid" BorderWidth="1px">
</asp:CheckBoxList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SqlConnectionString1 %>" SelectCommand="SELECT name FROM [students]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
Web.Config:
Code
<connectionStrings>
<add name="SqlConnectionString1" connectionString="Data Source=(local);Initial Catalog=test;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<connectionStrings>
<add name="SqlConnectionString1" connectionString="Data Source=(local);Initial Catalog=test;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>