在GridView里使用CheckBox
在GridView的第一列使用CheckBox控制每一行数据,是经常使用的,这里面我要记录的是全选、全消、选中行的底色更改,还有就是在提交数据的时候,取选中的行的编号等功能
aspx页面定义:
 <asp:GridView ID="GridView1" runat="server">
<asp:GridView ID="GridView1" runat="server"> <Columns>
         <Columns> <asp:TemplateField ShowHeader="False" HeaderText="选择">
             <asp:TemplateField ShowHeader="False" HeaderText="选择"> <ItemStyle Width="60px" Wrap="False" />
                 <ItemStyle Width="60px" Wrap="False" /> <HeaderTemplate>
                 <HeaderTemplate> <asp:CheckBox ID="CheckBox1" runat="server"></asp:CheckBox>
                     <asp:CheckBox ID="CheckBox1" runat="server"></asp:CheckBox> </HeaderTemplate>
                 </HeaderTemplate> <ItemTemplate>
                 <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server"></asp:CheckBox>
                     <asp:CheckBox ID="CheckBox1" runat="server"></asp:CheckBox> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("EventID") %>' Visible="false"></asp:TextBox>
                     <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("EventID") %>' Visible="false"></asp:TextBox> </ItemTemplate>
                 </ItemTemplate> </asp:TemplateField>
             </asp:TemplateField> </Columns>
         </Columns> <HeaderStyle Font-Bold="False" BackColor="#EEEEEE" />
        <HeaderStyle Font-Bold="False" BackColor="#EEEEEE" /> <AlternatingRowStyle BackColor="#EEEEEE" />
         <AlternatingRowStyle BackColor="#EEEEEE" /> </asp:GridView>
</asp:GridView>这样在GridView的第一列,是CheckBox控件,在HeaderTemplate里的是用来控制全选全消的,在TemplateField里的,用来控制当前行是不是选中行
js脚本是这个:
 <script type="text/javascript" language="javascript">
<script type="text/javascript" language="javascript"> function changecolor(obj,color)
function changecolor(obj,color) {
{ e = event.srcElement
    e = event.srcElement if(e.checked==true)
    if(e.checked==true) {
    { e = e.parentElement
      e = e.parentElement e.style.background = "#C0C0FF"
      e.style.background = "#C0C0FF" }
     } else
     else {
     { e = e.parentElement
       e = e.parentElement e.style.background = color
       e.style.background = color }
      } }
} 
 function CheckSelect(del)
function CheckSelect(del) {
{ var rt=true;
     var rt=true; var num=0;
     var num=0; var boxorder=0;
     var boxorder=0; var objCheckBox;
     var objCheckBox; for(var i=0;i<aspnetForm.length;i++)
     for(var i=0;i<aspnetForm.length;i++) {
     { ///alert(aspnetForm.elements[i].type);
         ///alert(aspnetForm.elements[i].type); if(aspnetForm.elements[i].type=="checkbox")
         if(aspnetForm.elements[i].type=="checkbox") {
         { boxorder++;
             boxorder++; objCheckBox=aspnetForm.elements[i];
             objCheckBox=aspnetForm.elements[i]; ///alert(objCheckBox.checked);
             ///alert(objCheckBox.checked); if(objCheckBox.checked)
             if(objCheckBox.checked) {
             { if(boxorder>1)
                 if(boxorder>1) {
                 { //如果是第一个CheckBox,是用来全选全消的,这个不算,不用自加
                     //如果是第一个CheckBox,是用来全选全消的,这个不算,不用自加 num++;
                     num++; }
                 } }
             } }
         } }
     } ///alert(num);
     ///alert(num); if(!del)
     if(!del) {
     { if(num>1)
         if(num>1) {
         { alert("此操作不允许选择多个事件");
             alert("此操作不允许选择多个事件"); rt=false;
             rt=false; }
         } }
     } if(num==0)
     if(num==0) {
     { alert("此操作要求先选择一个事件");
         alert("此操作要求先选择一个事件"); rt=false
         rt=false }
     } ///alert(rt);
     ///alert(rt); return rt;
     return rt; }
}
 function SelectAll(sa)
function SelectAll(sa) {
{ ///全选/全消
     ///全选/全消 var objCheckBox;
     var objCheckBox; for(var i=0;i<aspnetForm.length;i++)
     for(var i=0;i<aspnetForm.length;i++) {
     { if(aspnetForm.elements[i].type=="checkbox")
         if(aspnetForm.elements[i].type=="checkbox") {
         { objCheckBox=aspnetForm.elements[i];
             objCheckBox=aspnetForm.elements[i]; objCheckBox.checked=sa;
             objCheckBox.checked=sa; }
         } }
     } }
} </script>
</script>
第一个函数changecolor用来控制被选择的行的颜色突出,第二个函数CheckSelect用来判断现在有几个行被选中,第三个函数SelectAll用来控制全选全消
在cs页面里,为每一行的CheckBox绑定客户端事件:
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
     { if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
         if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow) {
         { CheckBox mycb = new CheckBox();
             CheckBox mycb = new CheckBox(); mycb = (CheckBox)e.Row.FindControl("CheckBox1");
             mycb = (CheckBox)e.Row.FindControl("CheckBox1"); if (mycb != null)
             if (mycb != null) {
             { if (e.Row.RowType == DataControlRowType.DataRow)
                 if (e.Row.RowType == DataControlRowType.DataRow) {
                 { if (e.Row.RowIndex % 2 == 0)
                    if (e.Row.RowIndex % 2 == 0) {
                     { mycb.Attributes.Add("onclick", "changecolor(this.name,'#FFFFFF')");
                         mycb.Attributes.Add("onclick", "changecolor(this.name,'#FFFFFF')"); }
                     } else
                     else {
                     { mycb.Attributes.Add("onclick", "changecolor(this.name,'#EEEEEE')");
                         mycb.Attributes.Add("onclick", "changecolor(this.name,'#EEEEEE')"); }
                     } }
                 } else if (e.Row.RowType == DataControlRowType.Header)
                 else if (e.Row.RowType == DataControlRowType.Header) {
                 { mycb.Attributes.Add("onclick", "SelectAll(this.checked)");
                     mycb.Attributes.Add("onclick", "SelectAll(this.checked)"); }
                 } }
             } }
         } }
     }这里,对不同的DataControlRowType,我绑定了不同的客户端事件,因为定义在HeaderTemplate和ItemTemplate里的CheckBox是要执行不同的客户端事件的
上面的东西都很简单,分清楚客户端和服务器端就可以了,我一开始的时候在服务器端和客户端跳来跳去,自己都乱了,好烦啊
我在这里还要记录这样的情况:
在页面里,有一个自定义用户控件,有一个GridView控件,在自定义用户控件里,有Button用来控制GridView里的记录
我要记录的是,怎么在这个Button里,判断有哪些记录被选中
1、上面的aspx代码里,我加了TextBox,记录编号
2、在自定义用户控件里,用下面的事件,就返回选中行的编号了
 public string GetEid()
public string GetEid() {
     { //取选中的事件编号
         //取选中的事件编号 string streid = "";
         string streid = ""; GridView mygv = new GridView();
         GridView mygv = new GridView(); mygv = (GridView)Parent.FindControl("GridView1");
         mygv = (GridView)Parent.FindControl("GridView1"); if (mygv != null)
         if (mygv != null) {
         { int i, row;
             int i, row; i = 0;
             i = 0; row = mygv.Rows.Count;
             row = mygv.Rows.Count; CheckBox mycb = new CheckBox();
             CheckBox mycb = new CheckBox(); for (i = 0; i < row; i++)
             for (i = 0; i < row; i++) {
             { mycb = (CheckBox)mygv.Rows[i].FindControl("CheckBox1");
                 mycb = (CheckBox)mygv.Rows[i].FindControl("CheckBox1"); if (mycb != null)
                 if (mycb != null) {
                 { if (mycb.Checked)
                     if (mycb.Checked) {
                     { TextBox mytb = new TextBox();
                         TextBox mytb = new TextBox(); mytb = (TextBox)mygv.Rows[i].FindControl("TextBox1");
                         mytb = (TextBox)mygv.Rows[i].FindControl("TextBox1"); if (mytb != null)
                         if (mytb != null) {
                         { streid = streid + mytb.Text.Trim() + ",";
                             streid = streid + mytb.Text.Trim() + ","; }
                         } }
                     } }
                 } }
             } }
         } if (streid.Length > 0)
         if (streid.Length > 0) {
         { streid = streid.Remove(streid.Length - 1);
             streid = streid.Remove(streid.Length - 1); }
         } return streid;
         return streid; }
     }就是Parent和FindControl的使用,用Parent可以引用包含自定义用户控件的页面,对这个页面里的GridView的每一行扫描,就可以取到每一行的CheckBox了
对自定义控件里,如果有的Button需要先选择一个 or 多个记录才允许操作的,可以在自定义控件里给Button加上客户端事件
 protected void Page_Load(object sender, EventArgs e)
protected void Page_Load(object sender, EventArgs e) {
     { Button1.Attributes["onclick"] = "return CheckSelect(false);";
         Button1.Attributes["onclick"] = "return CheckSelect(false);"; Button4.Attributes["onclick"] = "return CheckSelect(false);";
         Button4.Attributes["onclick"] = "return CheckSelect(false);"; Button2.Attributes["onclick"] = "return CheckSelect(true);";
         Button2.Attributes["onclick"] = "return CheckSelect(true);"; }
     }这里我给Button1和Button4加了不允许多个选择的事件,给Button2加了允许多个选择的事件
 
                    
                
 
     
                
            
         
 浙公网安备 33010602011771号
浙公网安备 33010602011771号