GRIDVIEW多行选择

1 . 页面代码 给GridView添加TemplateField ,在TemplateField 下添加
    CheckBoxList ,选他的原因是他有 AutoPostBack回送事件,当选择的时候可以发出回送,从而改变背景颜色,或者其它自定义函数,在次的函数是CheckBoxList1_TextChanged
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<html" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
            <Columns>
                <asp:TemplateField HeaderText="选择"> 
                <ItemTemplate>
                    <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" OnTextChanged="CheckBoxList1_TextChanged">
                    <asp:ListItem ></asp:ListItem>
                    </asp:CheckBoxList>
                 </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    </div>
    </form>
</body>
</html>
2。cs代码,数据源ArrayList
      protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ArrayList values = new ArrayList();
            values.Add(0);
            values.Add(1);
            values.Add(2);
            values.Add(3);
            values.Add(4);
            values.Add(5);
            values.Add(6);
            values.Add(2134);
            values.Add(123);
            values.Add(123);
            this.GridView1.DataSource = values;
            this.GridView1.DataBind();
        }
    }
    3 单击事件  找出选择的行
    protected void Button1_Click(object sender, EventArgs e)
    {
        CheckBoxList CheckBoxList1 = new CheckBoxList();
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            CheckBoxList1 = ((CheckBoxList)this.GridView1.Rows[i].FindControl("CheckBoxList1"));
            if (CheckBoxList1.Items[0].Selected)
            {
               Response.Write(this.GridView1.Rows[i].Cells[1].Text + "<br />");
            }
        }
    }
4 。CheckBoxList1改变的时候触发
    protected void CheckBoxList1_TextChanged(object sender, EventArgs e)
    {
        CheckBoxList CheckBoxList1 = new CheckBoxList();
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            CheckBoxList1 = ((CheckBoxList)this.GridView1.Rows[i].FindControl("CheckBoxList1"));
            if (CheckBoxList1.Items[0].Selected)
            {
                this.GridView1.Rows[i].BackColor = System.Drawing.Color.Tomato;
            }
            else
            {
                this.GridView1.Rows[i].BackColor = System.Drawing.Color.White;
            }
        }
   }
    
总结:交互多,还有选择时候的遍例都增加了时间,这也许就是windows程序和web的很大区别吧!
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值