GridView嵌套GridView的一些操作,包括取得主键,CHECKBOX全选等

原创 2007年09月20日 18:15:00
<%...@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

<!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">



<STYLE type=text/css>...BODY {...}{
    MARGIN-TOP
: 0px; FONT-SIZE: 9pt; MARGIN-LEFT: 4px; MARGIN-RIGHT: 0px; FONT-FAMILY: "宋体"
}

{...}{
    FONT-WEIGHT
: 400; FONT-SIZE: 13px; COLOR: black; TEXT-DECORATION: none
}

A:hover 
{...}{
    FONT-WEIGHT
: 400; FONT-SIZE: 13px; COLOR: red; TEXT-DECORATION: underline
}

A:active 
{...}{
    FONT
: 9pt "宋体"; CURSOR: hand; COLOR: #ff0033
}

</style>
    
<title>无标题页</title>

    
<script type="text/javascript">...

      
function show(tips,flag,url)...{
var my_tips=document.all.mytips;
if(flag)...{
    my_tips.style.display
="";
    
if (url!=null)...{my_tips.innerHTML="<img src='"+url+"'align=left>"+tips;}
    
else...{ my_tips.innerHTML=tips;}
    my_tips.style.left
=event.clientX+10;
    my_tips.style.top
=event.clientY+10;
   }

else 
  
...{
   my_tips.style.display
="none";
   }

}

    
</script>

</head>
<body>
    
<form id="form1" runat="server">
        
<div>
            
&nbsp;<table>
                
<tr>
                    
<td>
<href="#"  tips="请选择类别" onmousemove=show(this.tips,1) onmouseout=show(this.tips,0)>请选择类别</a>
                    
                        
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                            
<asp:ListItem>请选择</asp:ListItem>
                            
<asp:ListItem Value="部门">部门/科室</asp:ListItem>
                            
<asp:ListItem>学校</asp:ListItem>
                        
</asp:DropDownList>
                        
<asp:CheckBox ID="chk_All" runat="server" OnCheckedChanged="chk_All_CheckedChanged"
                            Text
="全选" AutoPostBack="True" />
                        
<asp:Button ID="btn_Submit" runat="server" OnClick="btn_Submit_Click" Text="确定" />
                        
                        
</td>
                    
<td>
                    
</td>
                
</tr>
                
<tr>
                    
<td>
                        
&nbsp;<asp:GridView ID="grv_DepartMent" runat="server" AutoGenerateColumns="False"
                            DataKeyNames
="DepartmentID" OnRowDataBound="grv_DepartMent_RowDataBound" OnRowCommand="grv_DepartMent_RowCommand">
                            
<Columns>
                                
<asp:BoundField DataField="DepartmentID" HeaderText="DepartmentID" />
                                
<asp:BoundField DataField="DepartMentName" HeaderText="部门名称" />
                                
<asp:TemplateField>
                                    
<EditItemTemplate>
                                        
<asp:CheckBox ID="CheckBox1" runat="server" />
                                    
</EditItemTemplate>
                                    
<ItemTemplate>
                                        
<asp:CheckBox ID="chk_SelectDepartment" runat="server" AutoPostBack="True" OnCheckedChanged="chk_SelectDepartment_CheckedChanged"
                                            Text
="选择该部门" />
                                    
</ItemTemplate>
                                
</asp:TemplateField>
                                
<asp:TemplateField>
                                    
<ItemTemplate>
                                        
<asp:GridView ID="grv_User" runat="server" AutoGenerateColumns="False" Width="100%">
                                            
<Columns>
                                                
<asp:BoundField DataField="RealName" HeaderText="用户名" />
                                                
<asp:TemplateField>
                                                    
<EditItemTemplate>
                                                        
<asp:CheckBox ID="CheckBox1" runat="server" />
                                                    
</EditItemTemplate>
                                                    
<ItemTemplate>
                                                        
<asp:CheckBox ID="chk_User" runat="server" />
                                                    
</ItemTemplate>
                                                    
<ItemStyle Width="15px" />
                                                
</asp:TemplateField>
                                            
</Columns>
                                        
</asp:GridView>
                                    
</ItemTemplate>
                                
</asp:TemplateField>
                            
</Columns>
                            
<EmptyDataTemplate>
                                
<asp:GridView ID="grv_User" runat="server" AutoGenerateColumns="False" ShowHeader="False">
                                    
<Columns>
                                        
<asp:BoundField DataField="RealName" />
                                        
<asp:TemplateField>
                                            
<EditItemTemplate>
                                                
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                                            
</EditItemTemplate>
                                            
<ItemTemplate>
                                                
<asp:CheckBox ID="chk_User" runat="server" Text="选择" />
                                            
</ItemTemplate>
                                        
</asp:TemplateField>
                                    
</Columns>
                                
</asp:GridView>
                            
</EmptyDataTemplate>
                        
</asp:GridView>
                    
</td>
                    
<td>
                        
<asp:DataList ID="dlst_DepartMent" runat="server" DataKeyField="DepartMentID" OnItemDataBound="dlst_DepartMent_ItemDataBound">
                            
<ItemTemplate>
                                
&nbsp;
                                
<table style="width: 100%; height: 100%">
                                    
<tr>
                                        
<td>
                                            
<asp:Label ID="lbl_DepartMentName" runat="server" Text='<%# Eval("DepartMentName") %>'></asp:Label>
                                            
<asp:CheckBox ID="chk_DepartMent" runat="server" AutoPostBack="True" OnCheckedChanged="chk_DepartMent_CheckedChanged" /></td>
                                    
</tr>
                                    
<tr>
                                        
<td>
                                            
<asp:GridView ID="grv_UserView" runat="server" AutoGenerateColumns="False" Width="100%">
                                                
<Columns>
                                                    
<asp:BoundField DataField="RealName" HeaderText="用户名" />
                                                    
<asp:TemplateField>
                                                        
<ItemStyle Width="15px" />
                                                        
<ItemTemplate>
                                                            
<asp:CheckBox ID="chk_SUser" runat="server" />
                                                        
</ItemTemplate>
                                                    
</asp:TemplateField>
                                                
</Columns>
                                            
</asp:GridView>
                                        
</td>
                                    
</tr>
                                    
<tr>
                                        
<td>
                                        
</td>
                                    
</tr>
                                
</table>
                            
</ItemTemplate>
                            
<HeaderTemplate>
                                
<asp:CheckBox ID="chk_SelectAll" runat="server" AutoPostBack="True" OnCheckedChanged="chk_SelectAll_CheckedChanged"
                                    Text

 

后台代码:

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BigOA.Logic;
using BigOA.Data;

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

        
if (Request["ID"!= null)
        
...{
 
        }

    }



    
/**//// <summary>
    
/// 用户列表
    
/// </summary>

    private void SetList()
    
...{
        
if (DropDownList1.SelectedIndex > 0)
        
...{
            chk_All.Enabled 
= true;

            
string dtype = DropDownList1.SelectedValue;
            BigOA.Data.User_BasicInfoTableAdapters.User_BasicInfo_SelectDTypeTableAdapter Adapter 
= new BigOA.Data.User_BasicInfoTableAdapters.User_BasicInfo_SelectDTypeTableAdapter();
            BigOA.Data.User_BasicInfo.User_BasicInfo_SelectDTypeDataTable Table 
= Adapter.GetDataByDType(dtype);
            Adapter.Dispose();

            dgv_SchoolDepartment.DataSource 
= Table;
            dgv_SchoolDepartment.DataBind();
            
        }

        
else
        
...{
            chk_All.Enabled 
= false;
        }

    }




    
private void LoadDepartMentData()
    
...{
        BigOA.Data.User_BasicInfoTableAdapters.User_BasicInfo_GetDepartmentTableAdapter adpt 
= new BigOA.Data.User_BasicInfoTableAdapters.User_BasicInfo_GetDepartmentTableAdapter();

        BigOA.Data.User_BasicInfo.User_BasicInfo_GetDepartmentDataTable table 
= adpt.GetDepartMentNameGetData();
        adpt.Dispose();

        grv_DepartMent.DataSource 
= table;
        grv_DepartMent.DataBind();

        dlst_DepartMent.DataSource 
= table;
        dlst_DepartMent.DataBind();
    }


    
private void LoadUserData(int DepartmentID, GridView grv)
    
...{
        BigOA.Data.User_BasicInfoTableAdapters.User_BasicInfoSimpleTableAdapter adpt 
= new BigOA.Data.User_BasicInfoTableAdapters.User_BasicInfoSimpleTableAdapter();
        BigOA.Data.User_BasicInfo.User_BasicInfoSimpleDataTable table 
= adpt.GetDataByDepartmentID(DepartmentID);

        grv.DataSource 
= table;
        grv.DataBind();        
    }


    
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    
...{
        SetList();
    }

    
protected void btn_Submit_Click(object sender, EventArgs e)
    
...{
        
foreach (GridViewRow grvRow in dgv_SchoolDepartment.Rows)
        
...{

            CheckBox chk 
= (CheckBox)grvRow.FindControl("chk_Select");
            
if (chk.Checked)
            
...{

            }

        }

    }

    
protected void chk_All_CheckedChanged(object sender, EventArgs e)
    
...{
        
bool flag = chk_All.Checked;
        
foreach (GridViewRow grvRow in dgv_SchoolDepartment.Rows)
        
...{

            CheckBox chk 
= (CheckBox)grvRow.FindControl("chk_Select");
            chk.Checked 
= flag;
        }

        
foreach (GridViewRow grvDeptRow in grv_DepartMent.Rows)
        
...{

            CheckBox chk 
= (CheckBox)grvDeptRow.FindControl("chk_SelectDepartment");
            chk.Checked 
= flag;

            GridView grv_User 
= (GridView)grvDeptRow.FindControl("grv_User");
            
foreach (GridViewRow grvUserRow in grv_User.Rows)
            
...{
                CheckBox chk_user 
= (CheckBox)grvUserRow.FindControl("chk_User");
                chk_user.Checked 
= flag;
            }

        }

    }


    
protected void grv_DepartMent_RowDataBound(object sender, GridViewRowEventArgs e)
    
...{
        
if (e.Row.RowType == DataControlRowType.DataRow)
        
...{
            
//获取gridview主键
            DataKey key = grv_DepartMent.DataKeys[e.Row.RowIndex];
            
//获取gridview一行中的数据
            DataRowView _view = (DataRowView)e.Row.DataItem;

            
string DepartmentID2 = _view["DepartmentID"].ToString();
            
int DepartmentID = Convert.ToInt32(key.Value);

            GridView grv 
= (GridView)e.Row.FindControl("grv_User");
            LoadUserData(DepartmentID, grv);

            
//gridview鼠标经过改变行背景色
            e.Row.Attributes.Add("onmouseover""this.style.backgroundColor='ivory'");

            e.Row.Attributes.Add(
"onmouseout""this.style.backgroundColor='white' ");



        }

    }

    
protected void grv_DepartMent_RowCommand(object sender, GridViewCommandEventArgs e)
    
...{

    }

    
protected void chk_SelectDepartment_CheckedChanged(object sender, EventArgs e)
    
...{
        CheckBox chk 
= (CheckBox)sender;
        
bool flag = chk.Checked;
        GridViewRow row 
= (GridViewRow)chk.NamingContainer;
        GridViewRow curRow 
= (GridViewRow)chk.Parent.Parent;
        DataKey key 
= grv_DepartMent.DataKeys[row.RowIndex];
        GridView grv 
= (GridView)row.FindControl("grv_User");
        
foreach (GridViewRow grvrow in grv.Rows)
        
...{
            CheckBox chk_user 
= (CheckBox)grvrow.FindControl("chk_User");
            chk_user.Checked 
= flag;
        }

    }

    
protected void dlst_DepartMent_ItemDataBound(object sender, DataListItemEventArgs e)
    
...{
        
if (e.Item.ItemType == ListItemType.Item)
        
...{
            
object key = dlst_DepartMent.DataKeys[e.Item.ItemIndex];
            
int DepartmentID = Convert.ToInt32(key);

            GridView grv 
= (GridView)e.Item.FindControl("grv_UserView");
            LoadUserData(DepartmentID, grv);
        }

    }

    
protected void chk_SelectAll_CheckedChanged(object sender, EventArgs e)
    
...{
        CheckBox chk_SelectAll 
= (CheckBox)sender;
        
bool flag = chk_SelectAll.Checked;
        
foreach (DataListItem item in dlst_DepartMent.Items)
        
...{
            CheckBox chk_DepartMent 
= (CheckBox)item.FindControl("chk_DepartMent");
            chk_DepartMent.Checked 
= flag;

            GridView grv_UserView
=(GridView)item.FindControl("grv_UserView");
            
foreach (GridViewRow row in grv_UserView.Rows)
            
...{
                CheckBox chk_SUser 
=(CheckBox) row.FindControl("chk_SUser");
                chk_SUser.Checked 
= flag;
            }


        }

    }

    
protected void chk_DepartMent_CheckedChanged(object sender, EventArgs e)
    
...{
        CheckBox chk_DepartMent 
= (CheckBox)sender;
        
bool flag = chk_DepartMent.Checked;
        DataLi

asp.net gridview中添加checkbox列,用于全部选中记录或取消全选

方法一:使用javascript代码控制                          前台代码:...
  • nvhaixx
  • nvhaixx
  • 2013年10月08日 14:03
  • 4590

Gridview的Item嵌套CheckBox的另一种实现,避免了选中错乱等问题

GridView或者ListView的item布局嵌套checkBox选中数据错乱的问题解决。此处实现了GridView 其实listview类似...
  • guiping_ding
  • guiping_ding
  • 2015年11月16日 18:26
  • 1502

ListView/GridView嵌套CheckBox滑动时CheckBox选中状态显示错乱

在讨论这个问题之前应该先了解ListView的复用机制  ListView复用的原理:ListView中的每一个Item显示都需要Adapter调用一次getView的方法,这个方法会传入一个con...
  • qq_30219217
  • qq_30219217
  • 2017年12月12日 16:13
  • 76

GridControl 选择列、复选框全选(上)

说明:     GirdControl 中添加一列,这一列不是写在数据库中的,而是代码中添加的。 图示:   底层类代码: #region GridControl 全选 /// ...
  • u013816709
  • u013816709
  • 2015年07月01日 17:20
  • 7101

ScrollView同时嵌套ListView和GridView怎么解决?(参考我上一片博文!!!!)

先看看最终的效果图吧:
  • logicsboy
  • logicsboy
  • 2014年06月08日 12:14
  • 699

GridView控件中添加CheckBox控件,并且实现选择、全选和反选

一、如何在GridView中添加CheckBox控件?      1、添加一个GridView控件后,单击向右箭头,选择【编辑列】;      2、在弹出的【字段】窗口中,选择TemplateFiel...
  • u010276845
  • u010276845
  • 2013年10月19日 20:09
  • 1149

Android实现ListView嵌套Checkbox真正的多选、全选、反选

Android实现ListView嵌套Checkbox真正的多选、全选、反选我们在开发APP的时候,很多情况下会使用到ListView嵌套CheckBox的情况,其实很多人要说这个其实很简单了,并没有...
  • u010151514
  • u010151514
  • 2016年03月21日 17:11
  • 3558

GridView实现多选,全选,反选等功能

先看看效果 我这里用到了数据库来储存选择的数据,数据库的简单实用我在前面的博客写过了,这里就不多说了,直接贴代码数据库操作类的代码package com.duanlian.gridviewmulti...
  • dl10210950
  • dl10210950
  • 2017年02月03日 16:16
  • 1932

ASP.NET的Gridview控件简单使用(分页、序号、选框、全选)(附源码)

这次我们实现的功能是:全选,单选,可以提取一行的数据,分页,结合下拉列表筛选数据,显示序号。1.全选 //全选 protected void CheckBox2_CheckedCh...
  • q393364227
  • q393364227
  • 2016年12月21日 17:31
  • 555

ListView嵌套GridView数据适配

package text.com.yuekaodemo4; import android.os.Handler; import android.os.Message; import an...
  • xiaoxing1992
  • xiaoxing1992
  • 2017年03月05日 21:02
  • 174
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:GridView嵌套GridView的一些操作,包括取得主键,CHECKBOX全选等
举报原因:
原因补充:

(最多只允许输入30个字)