张银的博客


Eat to live, but do not live to eat.

导航

复选列表(CheckBoxList)/单选列表(RadioButtonList)

Posted on 2009-01-07 18:39  张银  阅读(1318)  评论(0编辑  收藏  举报

CheckBoxList(复选列表)
1.如何自己添加项
2.布局方式(水平/垂直)(表格/流)(文字和框的位置)
3.读取数据库的内容
4.为数据绑定再追加一个静态项(比如“不选择”静态项目)注意行为里的AppendDataBoundItems属性
5.控件事件
6.读出被选中的

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default4 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {

    }
    
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
int n = CheckBoxList1.Items.Count;
        
for (int i = 0; i < n;i++ )
        {
            
if (CheckBoxList1.Items[i].Selected)
            {
                
string txt = CheckBoxList1.Items[i].Text;
                
string t = txt.Replace("aidd2008","火君道人");
                Response.Write(t);  
//CheckBoxList1的AutoPostBack?
            }
        }
    }
    
protected void Button1_Click(object sender, EventArgs e)
    {
        
if (CheckBoxList1.RepeatDirection == RepeatDirection.Vertical)
        {
            CheckBoxList1.RepeatDirection 
= RepeatDirection.Horizontal;
            Button1.Text 
= "给我垂直排列";
        }
        
else
        {
            CheckBoxList1.RepeatDirection 
= RepeatDirection.Vertical;
            Button1.Text 
= "给我水平排列";
        }
    }
    
protected void CheckBoxList1_DataBound(object sender, EventArgs e)
    {
        
int n = CheckBoxList1.Items.Count;
        
for (int i = 0; i < n; i++)
        {
            CheckBoxList1.Items[i].Text 
= CheckBoxList1.Items[i].Text + i;
        }
    }
}

 

    <form id="form1" runat="server">
    
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AppendDataBoundItems="True" AutoPostBack="True"
        DataSourceID
="SqlDataSource1" DataTextField="senduser" DataValueField="id" OnDataBound="CheckBoxList1_DataBound"
        OnSelectedIndexChanged
="CheckBoxList1_SelectedIndexChanged">
        
<asp:ListItem>不选择</asp:ListItem>
    
</asp:CheckBoxList>
    
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:2000data %>"
        SelectCommand
="SELECT TOP 5 id, filenames, senduser FROM redheadedfile"></asp:SqlDataSource>
    
</>
    
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="排列" />
    
</form>

 

    <connectionStrings>
        
<add name="2000data" connectionString="Data Source=.;Initial Catalog=data;Persist Security Info=True;User ID=i05;Password=i05630229" providerName="System.Data.SqlClient" />
    
</connectionStrings>


RadioButtonList(单选列表)
与CheckBoxList(复选列表)不同的是,单选列表是单选,复选列表是可以复选