关闭

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

标签: aspserverobjecttextboxusertable
1261人阅读 评论(0) 收藏 举报
<%@ 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
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13551次
    • 积分:218
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:5篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论