GridView中实现CheckBoxList选中更新数据库
2016-11-28 16:12
417 查看
Asp.Net Code:
Asp.Net cs Code加载绑定已选项
WebHelper公共方法
<asp:TemplateField HeaderText="(勾选后自动保存)"> <ItemStyle HorizontalAlign="Center" /> <ItemTemplate> <asp:CheckBoxList ID="chkSaleDepotItem" RepeatDirection="Horizontal" RepeatColumns="9" CssClass="tblWrap" runat="server" AutoPostBack="true" OnSelectedIndexChanged="chkSaleDepotItem_SelectedIndexChanged"> </asp:CheckBoxList> </ItemTemplate> </asp:TemplateField>
Asp.Net cs Code加载绑定已选项
protected void gvIndustry_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { HiddenField RowVal = e.Row.FindControl("hidIndustryID") as HiddenField; CheckBoxList chkSaleDepotItem = (CheckBoxList)e.Row.FindControl("chkSaleDepotItem"); Mall_IndustryCompany IsExist = icBL.SelectOne("CompanyID=" + LogonUser.CompanyID); chkSaleDepotItem.DataSource = new Depot().SelectMany<Unisoft.ICS.Model.Stock.DepotInfo>("DepotID ASC", "IsEnable=1 and ToCompanyID=" + this.LogonUser.CompanyID); chkSaleDepotItem.DataTextField = "DepotName"; chkSaleDepotItem.DataValueField = "DepotID"; chkSaleDepotItem.DataBind(); if (IsExist != null) { if ((IsExist.IndustryID == Convert.ToInt32(RowVal.Value))) { //绑定已选 if (!string.IsNullOrEmpty(IsExist.SaleDepotItem) && chkSaleDepotItem.Items.Count > 0) { string[] DepotSprit = IsExist.SaleDepotItem.Split(','); WebHelper.SetCheckBoxListSelectValue(chkSaleDepotItem, DepotSprit); } } } } }
/// <summary> /// 勾选、取消操作,自动更新到数据库 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void chkSaleDepotItem_SelectedIndexChanged(object sender, EventArgs e) { CheckBoxList DepotItem = (CheckBoxList)sender; string SelVal = WebHelper.GetCheckBoxListSelVal(DepotItem, "", ",", 1); if (SelVal.Length > 0) { GridViewRow iRow = (GridViewRow)((CheckBoxList)sender).Parent.Parent; int IndustryID = Convert.ToInt32(this.gvIndustry.DataKeys[iRow.RowIndex].Value); Mall_IndustryCompany model = icBL.SelectOne("IndustryID=" + IndustryID + " and CompanyID=" + LogonUser.CompanyID); model.SaleDepotItem = SelVal; icBL.Update(model); this.Message.Text = "已更新。"; } else { this.Message.Text = "操作失败!必选一个以上!"; } }
WebHelper公共方法
#region CheckBoxList获取、设置 /// <summary> /// 获取CheckBoxList选中的值 /// </summary> /// <param name="checkList">CheckBoxList控件 (必填)</param> /// <param name="strBox">内容2边字符 (选填) 如:|内容|</param> /// <param name="Sprit">分隔符 (必填) 如:, |</param> /// <param name="Flag">1 SelectValue,2 SelectText</param> /// <returns></returns> public string GetCheckBoxListSelVal(CheckBoxList checkList, string strBox, string Sprit, int Flag) { string SelVal = ""; for (int i = 0; i < checkList.Items.Count; i++) { if (checkList.Items[i].Selected) { SelVal += strBox + checkList.Items[i].Value + strBox + Sprit; } } if (SelVal.Length > 1) { SelVal = SelVal.Substring(0, SelVal.Length - 1); } return SelVal; } /// <summary> /// 绑定CheckBoxList值 /// </summary> /// <param name="ListItem">CheckBoxList控件</param> /// <param name="Sprit">需要选中的值 string[] Sprit=1,2,3,4,5</param> private static void SetCheckBoxListSelectValue(CheckBoxList ListItem, string[] Sprit) { foreach (string item in Sprit) { for (int i = 0; i < ListItem.Items.Count; i++) { if ((ListItem.Items[i].Value == item)) { ListItem.Items[i].Selected = true; } } } } #endregion
相关文章推荐
- 在GridView中加入CheckBox实现全选以及提交CheckBox选中的相关内容
- JS实现对gridview中的checkbox的选中个数记录,并在页面刷新时保持checkbox的状态 .
- gridview无数据源实现更新数据库(即断开更新数据库)
- 扩展CheckBoxList实现选中绑定
- 选中CheckBoxList第一项实现全选/反选CheckBoxList所有项
- 实现GridView翻页并且实现CheckBox选中功能的保持
- GridView控件与CheckBox结合,实现全选功能,同时实现删除选中的多行数据
- Flex List 用嵌入的ItemRenderer实现CheckBox选中功能
- 扩展CheckBoxList实现选中绑定
- gridview、listview适配器布局中带有checkbox实现的列表 checkbox只能有一项选中
- 实现gridview中checkbox 模板列单选操作和.cs中判断checkbox是否选中
- asp.net CheckBox从数据库读取数据设定是否选中 并更新
- DATAGRIDVIEW动态筛选数据,实现编辑之后,点击按钮更新,及删除选中的列到数据库。
- 扩展CheckBoxList实现选中绑定
- Gridview的Item嵌套CheckBox的另一种实现,避免了选中错乱等问题
- ASPxGridView实现checkbox全选反选(自带的ShowSelectCheckBOx)并获取选中checkbox对应的值集合
- RadioButtonList 和CheckBoxList根据后天数据库的值加载时选中其选项
- DeveXpress ASPxGridView实现checkbox全选反选(自带的ShowSelectCheckBOx)并获取选中checkbox对应的值集合
- GridView控件与CheckBox结合,实现全选功能,同时实现删除选中的多行数据 .
- ASP.NET 之 CheckBoxList在实际项目中的应用——高级查询、数据库读出默认选中