为GridView添加多选CheckBox列


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewCheckBoxs.aspx.cs"
    Inherits
="NetSamples.aspnet.GridViewCheckBoxs" 
%>

<!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>Untitled Page</title>

    
<script language="javascript">
    
function BtnApply_onclick() 
    
{
    
     
var GridViewFilter = document.getElementById("GridView1");
      
var rowi = new Array();
      
      
for(var i=1;i<GridViewFilter.rows.length;i++)
      
{
         
if(GridViewFilter.rows[i].cells[0].childNodes[0].checked)
         
{
            rowi[rowi.length] 
= i + "";
         }

      }

      
if(rowi.length != 0)
      
{
         
if(rowi.length > 5)
         
{
            alert(
"最多不能超过5种!");
            
return false;
         }

      }

      
else
      
{
        alert(
"请选择产品!");
        
return false;
      }

}

    
function SelectAllCheckboxes(spanChk){
 
var oItem = spanChk.children;
 
var theBox = (spanChk.type == "checkbox")?spanChk:spanChk.children.item[0];
 xState 
= theBox.checked;
 
var elm = theBox.form.elements;
 
for(i=0;i<elm.length;i++)
 
if(elm[i].type == "checkbox")
 
{
  
if(elm[i].checked != xState)
  elm[i].click();
 }

 
}

    
</script>

</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
            
<Columns>
                
<asp:TemplateField HeaderText="全选">
                    
<HeaderTemplate>
                        
<input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" type="checkbox" />全选
                    
</HeaderTemplate>
                    
<FooterTemplate>
                        
<input id="chkAll1" runat="server" onclick="javascript:SelectAllCheckboxes(this);"
                            type
="checkbox" />全选 </HeaderTemplate>
                    
</FooterTemplate>
                    
<ItemTemplate>
                        
<asp:CheckBox ID="CheckBox1" runat="server" />
                    
</ItemTemplate>
                    
<ItemStyle HorizontalAlign="Left" />
                
</asp:TemplateField>
            
</Columns>
        
</asp:GridView>
        
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand
="select top 20 * from products"></asp:SqlDataSource>
        
<asp:Button ID="ButtonApply" runat="server" Text="申请所选" OnClick="ButtonApply_Click"
            OnClientClick
="javascript:return (BtnApply_onclick()!=false)" />
    
</div>
    
</form>
</body>
</html>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

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

        }
        
protected void ButtonApply_Click(object sender, EventArgs e)
        {
            List
<string> selectList = new List<string>();
            
int count = this.GridView1.Rows.Count;
            
for (int i = 0; i < count; i++)
            {
                Control ctrl 
= this.GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
                CheckBox cb 
= ctrl as CheckBox;
                
if (cb != null)
                {
                    
if (cb.Checked)
                    {
                        selectList.Add(
this.GridView1.Rows[i].Cells[1].Text);
                    }
                }
            }

            
//获取选中项
            string ss = "您选中了以下项:";
            
for (int i = 0; i < selectList.Count; i++)
            {
                ss 
+= selectList[i] + "";
            }
            Response.Write(ss);
        }
    }
}

posted on 2008-07-10 21:01  王丹小筑  阅读(1106)  评论(0)    收藏  举报

导航