在项目中,进行设定考核对象的时候。如果有已经设定的考核对象,那么对已经设定的考核对象复选框进行勾选。如果没有设定,可进行勾选设定考核对象用到CheckboxListbox ,在这里用到CheckboxListbox 。简单来介绍一下。
<div title="开发区(园区)" style="padding: 10px"> <asp:UpdatePanel ID="UpdatePanel3" runat="server"> <ContentTemplate> <div> <asp:Button ID="cmdSelectedDevelopment" runat="server" Text="提交" ToolTip="提交选择的考核对象" OnClick="cmdSelectedDevelopment_Click" Style="height: 20px" /> </div> <asp:CheckBox ID="SelectAll" runat="server" Text="全选" OnClick="selectAll('cblDevelopmentInfo')" /> <asp:CheckBoxList ID="cblDevelopmentInfo" RepeatDirection="Horizontal" RepeatColumns="5" runat="server"></asp:CheckBoxList> </ContentTemplate> </asp:UpdatePanel> </div>
<%--全选checkboxlist--%> <script type="text/javascript"> function selectAll(searchName) { var aa = document.getElementsByTagName("input"); for (var i = 0; i < aa.length; i++) { if (aa[i].type == 'checkbox' && aa[i].name.indexOf(searchName) > -1) { aa[i].checked = event.srcElement.checked; } } } </script>
//页面首次加载的时候,绑定数据 if (!IsPostBack == true) { this.cblDevelopmentInfo.DataSource = development.GetDevelopmentInfo(); this.cblDevelopmentInfo.DataTextField = "DepartmentName"; //前台看到的值,也就是CheckBoxList中显示出来的值 this.cblDevelopmentInfo.DataValueField = "CityID"; //这个值直接在页面上是看不到的,但在源代码中可以看到 this.cblDevelopmentInfo.DataBind(); } //对选择的Checboxlist进行相关的数据库操作 //获取已经选择的考核对象 DataSet dtCheckedDepartInfo = development.GetList("Checked='Yes' and Status='Active'"); // 外循环是获取选择考核对象 for (int i = 0; i < dtCheckedDepartInfo.Tables[0].Rows.Count; i++) { //已经选择的考核对象字段 string strCheckedCity = dtCheckedDepartInfo.Tables[0].Rows[i]["CityID"].ToString(); for (int j = 0; j < cblDevelopmentInfo.Items.Count; j++) { //全部的考核对象 string strUnCheckedCity = cblDevelopmentInfo.Items[j].Value.ToString(); if (strCheckedCity == strUnCheckedCity) { cblDevelopmentInfo.Items[j].Selected = true; break; } } } 将选择的更新到数据库中 #region 开发区Tab中 单击提交按钮,提交选择的考核对象到数据库中 protected void cmdSelectedDevelopment_Click(object sender, EventArgs e) { EvaluationSystem.Model.DevelopmentBaseEntity developmententity = new DevelopmentBaseEntity(); //实例化对应的实体类 EvaluationSystem.BLL.DevelopmentBaseBLL developmentrecord = new DevelopmentBaseBLL();//实例化对应的B层业务逻辑 developmentrecord.UpdateNoSelected(); for (int i = 0; i < this.cblDevelopmentInfo.Items.Count; i++) //通过循环 { if (this.cblDevelopmentInfo.Items[i].Selected)//获取控件中选定的项 { developmententity.CityID = Convert.ToString(this.cblDevelopmentInfo.Items[i].Value); // 获取实体ID if (developmentrecord.UpdateChecked(developmententity.CityID) == true)// 如果添加成功,进行提示 { ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click", "alert('已成功设定考核对象')", true); this.cblDevelopmentInfo.Items[i].Selected = true; SelectAll.Checked = false; } else { ScriptManager.RegisterClientScriptBlock(UpdatePanel3, this.GetType(), "click", "alert('设定考核对象失败,请联系系统管理员')", true); } } } } #endregion
这里的Checkboxlist结合Asp.net中自带的UpdataPannel实现了项目中的一个小业务功能。仔细想想,无非是控件的一些基本属性,还有就是JS对html的相关操作,然后结合循环结构,从而实现要求。