ListView全选和反选

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListView.aspx.cs" Inherits="ListView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<script type="text/javascript">
    function SelectAll(box) {
        for (var i = 0; i < document.forms['aspnetForm'].elements.length; i++) {
            var e = document.forms['aspnetForm'].elements[i];
            if ((e.type == 'checkbox')) {
                var o = e.name.lastIndexOf('CheckBox1');
                if (o != -1) {
                    e.checked = box.checked;
                }
            }
        }
    }
</script>
<body>
    <form id="aspnetForm" runat="server">
    <div>
        <asp:ListView ID="ListView1" runat="server">
            <ItemTemplate>
                <tr>
                    <td align="center">
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </td>
                    <td align="center">
                        <%# Eval("StuId")%>
                    </td>
                    <td align="center">
                        <%#Eval("StuName") %>
                    </td>
                    <td align="center">
                        <%#Eval("StuSex") %>
                    </td>
                </tr>
            </ItemTemplate>
            <EmptyDataTemplate>
                <table>
                    <tr>
                        <td align="center" style="color: Red">
                            无信息!
                        </td>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <LayoutTemplate>
                <div align="center">
                    <table>
                        <thead>
                            <tr>
                                <th style="width: 10%">
                                    <asp:CheckBox ID="CheckBox2" runat="server" onclick="SelectAll(this)" />
                                </th>
                                <th style="width: 10%">
                                    <p>
                                        学号</p>
                                </th>
                                <th style="width: 10%">
                                    <p>
                                        姓名</p>
                                </th>
                                <th style="width: 10%">
                                    <p>
                                        性别</p>
                                </th>
                            </tr>
                        </thead>
                        <tr id="itemPlaceholder" runat="server">
                        </tr>
                    </table>
                </div>
            </LayoutTemplate>
        </asp:ListView>
    </div>
    </form>
</body>
</html>

后台代码:

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

public partial class ListView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListViewDataBind();
        }
    }

    protected void ListViewDataBind()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("StuId");
        dt.Columns.Add("StuName");
        dt.Columns.Add("StuSex");
        for (int i = 0; i < 10; i++)
        {
            DataRow dr = dt.NewRow();
            dr["StuId"] = i;
            dr["StuName"] = "小东" + i;
            dr["StuSex"] = "男";
            dt.Rows.Add(dr);
        }
        ListView1.DataSource = dt;
        ListView1.DataBind();
    }
}
posted @ 2012-01-06 15:45  Damon201611  阅读(838)  评论(0编辑  收藏  举报