GridView的全选/取消全选(二)--完美联动版(源码,截图)

特点:
1.与http://www.126.com/邮箱的效果相当。GridView的行与CheckBox,CheckBox列自身(与列头checkbox)实现联动选择效果。
2.实用行强,界面友好,用户用好。
截图如下:
r_Gridview选择联动版1.bmp
有一个行未选择

r_Gridview选择联动版2.bmp
点击未选的行后(全选)


代码如下:

ContractedBlock.gif ExpandedBlockStart.gif dszq.aspx
  1None.gif<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dszq.aspx.cs" Inherits="dszq" %>
  2None.gif
  3None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4None.gif<html xmlns="http://www.w3.org/1999/xhtml">
  5None.gif<head runat="server">
  6None.gif    <title>GridView选择联动版-东山再起-</title>
  7None.gif</head>
  8None.gif<body>
  9None.gif    <form id="form1" runat="server">
 10None.gif        <div>
 11None.gif            <asp:GridView ID="mygridview" PageSize="20" runat="server" AutoGenerateColumns="False"
 12None.gif                BorderWidth="0px" BackColor="#6ba5d8" CellPadding="2" CellSpacing="1" Font-Size="12px"
 13None.gif                Width="100%" OnRowDataBound="mygridview_RowDataBound" EmptyDataText="暂无信息!">
 14None.gif                <Columns>
 15None.gif                    <asp:BoundField DataField="A" HeaderText="A" />
 16None.gif                    <asp:BoundField DataField="B" HeaderText="B" />
 17None.gif                    <asp:BoundField DataField="C" HeaderText="C" />
 18None.gif                    <asp:BoundField DataField="D" HeaderText="D" />
 19None.gif                    <asp:TemplateField>
 20None.gif                        <HeaderTemplate>
 21None.gif                            <font face="宋体">
 22None.gif                                <asp:CheckBox ID="CheckBox2" onclick="SelectAll(this);" runat="server"></asp:CheckBox></font>
 23None.gif                        </HeaderTemplate>
 24None.gif                        <ItemTemplate>
 25None.gif                            <asp:CheckBox ID="CheckBox1" Height="100%" runat="server" Width="100%"></asp:CheckBox>
 26None.gif                        </ItemTemplate>
 27None.gif                        <ItemStyle Width="20px" />
 28None.gif                    </asp:TemplateField>
 29None.gif                </Columns>
 30None.gif                <RowStyle BackColor="White" Height="20px" HorizontalAlign="Center" VerticalAlign="Middle"
 31None.gif                    Wrap="False" />
 32None.gif                <EmptyDataRowStyle BackColor="#EFF3FB" HorizontalAlign="Center" Height="20px" />
 33None.gif                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
 34None.gif                <PagerStyle BackColor="#E0E0E0" HorizontalAlign="Right" />
 35None.gif                <HeaderStyle BackColor="#6699cc" Font-Bold="True" ForeColor="White" Height="22px" />
 36None.gif                <AlternatingRowStyle BackColor="#EFF3FB" />
 37None.gif            </asp:GridView>
 38None.gif        </div>
 39None.gif    </form>
 40None.gif</body>
 41None.gif</html>
 42None.gif
 43None.gif<script type="text/javascript">
 44ExpandedBlockStart.gifContractedBlock.gif    /**//
 45InBlock.gif    ///GridView-东山再起/////
 46ExpandedBlockEnd.gif    /2007-10-20//

 47None.gif    var e;           
 48None.gif    //鼠标移入
 49None.gif    function mouseOver(row,nowRadio)
 50ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
 51InBlock.gif            e=row.style.backgroundColor; 
 52InBlock.gif            row.style.backgroundColor='#99ccff';
 53ExpandedBlockEnd.gif        }

 54None.gif        
 55None.gif    //鼠标移出
 56None.gif    function mouseOut(row,rowindex,nowRadio)
 57ExpandedBlockStart.gifContractedBlock.gif    dot.gif{   
 58InBlock.gif        if(nowRadio.checked==true)
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 60InBlock.gif            row.style.backgroundColor='#99ccff';
 61ExpandedSubBlockEnd.gif        }

 62InBlock.gif        else 
 63ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 64InBlock.gif            if(rowindex%2==0)
 65ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 66InBlock.gif                row.style.backgroundColor='AliceBlue';
 67ExpandedSubBlockEnd.gif            }

 68InBlock.gif            else 
 69ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 70InBlock.gif                 row.style.backgroundColor='#ffffff';
 71ExpandedSubBlockEnd.gif            }

 72ExpandedSubBlockEnd.gif        }

 73ExpandedBlockEnd.gif    }
  
 74None.gif    
 75ExpandedBlockStart.gifContractedBlock.gif    /**//// 点击行选择checkbox效果
 76None.gif      function setCheck(row,rowIndex,nowCheckbox) //行,行号,选择框
 77ExpandedBlockStart.gifContractedBlock.gif               dot.gif
 78InBlock.gif                  nowCheckbox.checked=!(nowCheckbox.checked);
 79InBlock.gif                  if( nowCheckbox.checked==false)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif                  dot.gif{
 81InBlock.gif                     if(rowIndex%2==0)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif                         dot.gif{
 83InBlock.gif                        row.style.backgroundColor='AliceBlue';
 84ExpandedSubBlockEnd.gif                         }

 85InBlock.gif                   else 
 86ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 87InBlock.gif                         row.style.backgroundColor='#ffffff';
 88ExpandedSubBlockEnd.gif                         }

 89ExpandedSubBlockEnd.gif                  }

 90InBlock.gif                  else 
 91ExpandedSubBlockStart.gifContractedSubBlock.gif                  dot.gif{
 92InBlock.gif                    row.style.background='#99ccff'
 93ExpandedSubBlockEnd.gif                  }

 94InBlock.gif                  
 95InBlock.gif                  //---------------用于选择与全选的一致性
 96InBlock.gif                  var ck2;
 97InBlock.gif                  var myForm=document.forms[0];
 98InBlock.gif                  for(var i=0;i<myForm.length;i++)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif                   dot.gif
100InBlock.gif                    objRadio=myForm.elements[i];
101InBlock.gif                    if(objRadio.type=="checkbox" &&objRadio.name.indexOf("CheckBox2")>-1)
102ExpandedSubBlockStart.gifContractedSubBlock.gif                       dot.gif{
103InBlock.gif                           ck2=objRadio;
104ExpandedSubBlockEnd.gif                      }

105ExpandedSubBlockEnd.gif                   }
                               
106InBlock.gif                  var contact=IsContact(nowCheckbox);  
107InBlock.gif                  if(contact==true)
108ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{ck2.checked=true; }
109InBlock.gif                  else 
110ExpandedSubBlockStart.gifContractedSubBlock.gif                   dot.gif{ ck2.checked=false; }
111InBlock.gif                  //------------------
112ExpandedBlockEnd.gif               }

113None.gif               
114None.gif               
115None.gif    function IsContact(nowRadio)
116ExpandedBlockStart.gifContractedBlock.gif               dot.gif
117InBlock.gif               contact=true;
118InBlock.gif               nowstate= nowRadio.checked;
119InBlock.gif                var myForm,objRadio;
120InBlock.gif                myForm=document.forms[0];
121InBlock.gif                for(var i=0;i<myForm.length;i++)
122ExpandedSubBlockStart.gifContractedSubBlock.gif                  dot.gif{
123InBlock.gif                    if(myForm.elements[i].type=="checkbox")
124ExpandedSubBlockStart.gifContractedSubBlock.gif                       dot.gif{
125InBlock.gif                        objRadio=myForm.elements[i];
126InBlock.gif                        if(objRadio!=nowRadio  && objRadio.name.indexOf("CheckBox1")>-1
127ExpandedSubBlockStart.gifContractedSubBlock.gif                          dot.gif{
128InBlock.gif                            if(objRadio.checked!=nowstate)
129ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
130InBlock.gif                                return false;
131ExpandedSubBlockEnd.gif                            }

132ExpandedSubBlockEnd.gif                        }

133ExpandedSubBlockEnd.gif                    }

134ExpandedSubBlockEnd.gif                }

135InBlock.gif                
136InBlock.gif                return nowstate;
137InBlock.gif                
138ExpandedBlockEnd.gif            }
           
139None.gif               
140None.gif               
141None.gif    
142ExpandedBlockStart.gifContractedBlock.gif    /**////checkbox 取反
143None.gif  function SelectAll(tempControl)
144ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
145InBlock.gif     //将头模板中的其它所有的 checkbox 取反
146InBlock.gif    //alert(tempControl.parentElement.parentElement.parentElement.parentElement.parentElement.id);
147InBlock.gif     var   elem=tempControl.parentElement.parentElement.parentElement.parentElement.parentElement.getElementsByTagName("input");
148InBlock.gif     var theBox=tempControl;
149InBlock.gif     xState=theBox.checked;   
150InBlock.gif     for(i=0;i<elem.length;i++)
151InBlock.gif     if(elem[i].type=="checkbox" && elem[i].id!=theBox.id)
152ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif
153InBlock.gif      if(elem[i].checked!=xState)
154InBlock.gif        elem[i].click();
155InBlock.gif         // elem[i].checked=xState;
156ExpandedSubBlockEnd.gif     }

157InBlock.gif
158ExpandedBlockEnd.gif    }
      
159None.gif
160None.gif</script>
161None.gif
162None.gif


 

ContractedBlock.gif ExpandedBlockStart.gif dszq.aspx.cs
 1None.gifusing System;
 2None.gifusing System.Data;
 3None.gifusing System.Configuration;
 4None.gifusing System.Collections;
 5None.gifusing System.Web;
 6None.gifusing System.Web.Security;
 7None.gifusing System.Web.UI;
 8None.gifusing System.Web.UI.WebControls;
 9None.gifusing System.Web.UI.WebControls.WebParts;
10None.gifusing System.Web.UI.HtmlControls;
11None.gif
12None.gifpublic partial class dszq : System.Web.UI.Page
13ExpandedBlockStart.gifContractedBlock.gifdot.gif{
14ContractedSubBlock.gifExpandedSubBlockStart.gif    Page_Load#region Page_Load
15InBlock.gif    protected void Page_Load(object sender, EventArgs e)
16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
17InBlock.gif        if (!IsPostBack)
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19InBlock.gif            GVBind();
20ExpandedSubBlockEnd.gif        }

21ExpandedSubBlockEnd.gif    }

22ExpandedSubBlockEnd.gif    #endregion

23InBlock.gif
24ContractedSubBlock.gifExpandedSubBlockStart.gif    GVBind#region GVBind
25InBlock.gif    protected void GVBind()
26ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
27InBlock.gif        DataTable dt = new DataTable();
28InBlock.gif        dt.Columns.Add("A"typeof(string));
29InBlock.gif        dt.Columns.Add("B"typeof(string));
30InBlock.gif        dt.Columns.Add("C"typeof(string));
31InBlock.gif        dt.Columns.Add("D"typeof(string));
32InBlock.gif
33InBlock.gif        for (int i = 0; i < 10; i++)
34ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
35InBlock.gif            DataRow dr = dt.NewRow();
36InBlock.gif            dr[0= "A" + i.ToString();
37InBlock.gif            dr[1= "B" + i.ToString();
38InBlock.gif            dr[2= "C" + i.ToString();
39InBlock.gif            dr[3= "D" + i.ToString();
40InBlock.gif            dt.Rows.Add(dr);
41ExpandedSubBlockEnd.gif        }

42InBlock.gif        mygridview.DataSource = dt.DefaultView;
43InBlock.gif        mygridview.DataBind();
44ExpandedSubBlockEnd.gif    }

45ExpandedSubBlockEnd.gif    #endregion

46InBlock.gif
47ContractedSubBlock.gifExpandedSubBlockStart.gif    RowDataBound#region RowDataBound
48InBlock.gif    protected void mygridview_RowDataBound(object sender, GridViewRowEventArgs e)
49ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
50InBlock.gif        if (e.Row.RowIndex != -1)
51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
52InBlock.gif            CheckBox checkbox1 = ((CheckBox)(e.Row.Cells[3].FindControl("CheckBox1")));
53InBlock.gif            e.Row.Attributes.Add("onclick""setCheck(this," + e.Row.RowIndex + "," + checkbox1.ClientID + ")");
54InBlock.gif
55InBlock.gif            //给每个checkbox绑定setCheck事件(JS)
56InBlock.gif            checkbox1.Attributes.Add("onclick""setCheck(" + e.Row.ClientID + "," + e.Row.RowIndex + ",this)");
57InBlock.gif            checkbox1.Attributes.Add("ondblclick"" return false;");//但是没有避免掉默认的执行行事件
58InBlock.gif
59InBlock.gif            //鼠标经过Row时的效果
60InBlock.gif            string Index = Convert.ToString(e.Row.RowIndex + 1); // 颜色错位了,用这个来调节的
61InBlock.gif            e.Row.Attributes.Add("onmouseover""mouseOver(this," + checkbox1.ClientID + ")");
62InBlock.gif            e.Row.Attributes.Add("onmouseout""mouseOut(this," + Index + "," + checkbox1.ClientID + ")");
63InBlock.gif
64InBlock.gif            e.Row.Attributes["style"= "cursor:hand";
65InBlock.gif
66ExpandedSubBlockEnd.gif        }

67ExpandedSubBlockEnd.gif    }

68ExpandedSubBlockEnd.gif    #endregion

69ExpandedBlockEnd.gif}

70None.gif

示例下载
声明:本人源码,无私贡献,帮助需要的人。如果转载请注明出处。

转载于:https://www.cnblogs.com/jdmei520/archive/2008/07/30/1256219.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值