---
title: "一个根据数据库中数据，并且页面中用户的勾选所达到的效果"
author: "jandroid"
date: "2009-12-24T14:26:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2011-11-04T13:06:48Z"
id: 1235097
url: "https://jandroid.iteye.com/blog/1235097"
---

**我在做培训功能的时候，牵涉到，针对培训期次，有已参加，未参加的人员，当针对某一期次的培训进行人员添加或删除时，根据实际情况，已参加就被勾选，而且要根据用户再次勾选或者取消来实时统计针对该批次已添加几个人，是否超过计划人数?**

实现效果：

![](http://p.blog.csdn.net/images/p_blog_csdn_net/yinyuan1987/EntryImages/20091224/xiaoguo.jpg)

页面JS:

&lt;script&gt;

&lt;!--

function addPerson()

{

var i,count=0;

for(i=0;i&lt;document.all.gvMessageEmp.rows.length;i++)

{

var cb=document.all.gvMessageEmp.rows(i).cells(0).children(0);

if(cb.checked)

{

count=count+1;

}

}

document.getElementById("lblTrainPersonsNow").value=String(count);

}

--&gt;

&lt;/script&gt;

在后台PageLoad方法中，下面这个是用来在针对期次加载时来初始化已参加人员的勾选情况

for (int i = 0; i &lt; j; i++)

{

CheckBox checkBox = (CheckBox)gvMessageEmp.Rows\[i].FindControl("chbID");

string empID = gvMessageEmp.Rows\[i].Cells\[1].Text.ToString().Trim();

string trainID = Session\["trainID"].ToString();

if (HRH\_TrainInfoDetailManager.GetHRH\_TrainInfoDetailByTrainID(trainID, empID) != 0)

{

checkBox.Checked = true;

}

}

下面是用来处理添加员工和取消员工的勾选情况

try{

int j = gvMessageEmp.Rows.Count;

int countCheck = 0;

if (Session\["trainID"] != null)

{

string trainID = Session\["trainID"].ToString();

HRH\_TrainClassInfo hRH\_TrainClassInfo = HRH\_TrainClassInfoManager.GetHRH\_TrainClassInfoByTrainID(trainID);

//这里钱是根据培训费用/计划人数得到的

decimal trainDtMoney = hRH\_TrainClassInfo.TrainMoney/ hRH\_TrainClassInfo.TrainPersons;

//只有当培训状态为：准备状态时，才可以对该培训计划进行人员的添加，其他状态均不可以

if (hRH\_TrainClassInfo.TrainState == 1)

{

for (int i = 0; i &lt; j; i++)

{

CheckBox checkBox = (CheckBox)gvMessageEmp.Rows\[i].FindControl("chbID");

string empID = gvMessageEmp.Rows\[i].Cells\[1].Text.ToString().Trim();

//当该条记录已经勾选，并且数据库中没有数据是，进行添加；

//否则,当记录没有勾选，并且数据中存在该记录，进行删除

if (checkBox.Checked && HRH\_TrainInfoDetailManager.GetHRH\_TrainInfoDetailByTrainID(trainID, empID) == 0)

{

countCheck = countCheck + 1;

//string empID = gvMessageEmp.Rows\[i].Cells\[1].Text.ToString().Trim();

string empName = gvMessageEmp.Rows\[i].Cells\[2].Text.ToString().Trim();

int depID = int.Parse(gvMessageEmp.Rows\[i].Cells\[3].Text.ToString().Trim());

string depName = gvMessageEmp.Rows\[i].Cells\[4].Text.ToString().Trim();

int trainDtState = 0;

HRH\_TrainInfoDetailManager.AddHRH\_TrainInfoDetail(new HRH\_TrainInfoDetail(trainID, empID, empName, depID, depName, trainDtMoney, trainDtState));

}

else if (!checkBox.Checked && HRH\_TrainInfoDetailManager.GetHRH\_TrainInfoDetailByTrainID(trainID, empID) &gt; 0)

{

HRH\_TrainInfoDetailManager.DeleteHRH\_TrainInfoDetail(trainID, empID);

}

}

if (countCheck &gt; Convert.ToInt32(lblTrainPersons.Text.ToString()))

{

string script = "&lt;script&gt;alert('您一共添加了" + countCheck.ToString() + "人,已经添加完所有的计划人数的名额！')&lt;/script&gt;";

this.ClientScript.RegisterStartupScript(this.GetType(), "", script);

Response.Redirect("InsertTrainPerson.aspx");

}

else

{

Response.Redirect("TrainPersonStateM.aspx");

}

//可以添加这期的培训编号，在进入培训人员管理页面时默认显示当前刚添加后的这期培训人员的情况

}

else

{

Page.ClientScript.RegisterStartupScript(GetType(), "", "&lt;script language=javascript&gt;alert('对不起，该培训计划已经完成或放弃，不能再添加或去除培训人员！');window.location.href='TrainClassInfoManager.aspx';&lt;/script&gt;");

}

}

}