您的位置:首页 > 数据库

GridView中实现CheckBoxList选中更新数据库

2016-11-28 16:12 417 查看
Asp.Net Code:

<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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐