CheckBoxList控件小用,SqlDataSource数据源

ContractedBlock.gif ExpandedBlockStart.gif 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>

 

Web.Config:

ContractedBlock.gif ExpandedBlockStart.gif Code
<connectionStrings>
        
<add name="SqlConnectionString1" connectionString="Data Source=(local);Initial Catalog=test;Integrated Security=True" providerName="System.Data.SqlClient"/>
 </connectionStrings>

转载于:https://www.cnblogs.com/jianlinglo/archive/2009/01/15/1376258.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值