后台代码:
//向右侧品牌组里添加品牌数据
protected void btnInput_Click(object sender, EventArgs e)
{
scriptinfo.InnerHtml = "";
if (lboxExist.Items.Count > 0)
{
int iselectcount = 0;
for (int j = 0; j < lboxExist.Items.Count; j++)
{
if (lboxExist.Items[j].Selected == true)
{
iselectcount++;
}
}
if (iselectcount == 0)
{
scriptinfo.InnerHtml = "<script>alert('请选择需要右移的品牌!')</script>";
return;
}
string Brandid = ""; //品牌id
string BGroupid = ""; //品牌组id
BGroupid = ddlNewinfo.SelectedValue;
for (int i = 0; i < lboxExist.Items.Count; i++)
{
ListItem item = lboxExist.Items[i];
if (lboxExist.Items[i].Selected == true)
{
Brandid = lboxExist.SelectedValue;
try
{
//向品牌与品牌组关系表中添加数据--> 判断重复先
string strsql = "select * from BranbGroup_Relation where BGroupid = " + BGroupid + " and Brandid = " + Brandid;
using (DataSet MySet = DbHelperSQL.Query(strsql))
{
if (MySet.Tables[0].Rows.Count == 0)
{
lBoxNew.Items.Add(item);
string BGName = ddlNewinfo.SelectedItem.Text;
string sql = "insert into BranbGroup_Relation (BGroupid,Brandid) values (" + BGroupid + "," + Brandid + ")";
int iResult = DbHelperSQL.ExecuteSql(sql);
if (iResult == 1)
{
lboxExist.Items[i].Selected = false;
continue;
}
}
}
}
catch (Exception ex)
{
scriptinfo.InnerHtml = "<script>alert('" + ex.Message + "')</script>";
return;
}
}
}
}
else
{
scriptinfo.InnerHtml = "<script>alert('左侧品牌组中没有品牌信息!')</script>";
return;
}
}
//以品牌组的形式-->向右侧品牌组里添加品牌数据
protected void btnGroupInput_Click(object sender, EventArgs e)
{
scriptinfo.InnerHtml = "";
if (lboxExist.Items.Count > 0)
{
string Brandid = ""; //品牌id
string BGnewroupid = ""; //右侧品牌组id
BGnewroupid = ddlNewinfo.SelectedValue;
for (int i = 0; i < lboxExist.Items.Count; i++)
{
ListItem item = lboxExist.Items[i];
Brandid = item.Value;
try
{
//向品牌与品牌组关系表中添加数据--> 判断重复先
string strsql = "select * from BranbGroup_Relation where BGroupid = " + BGnewroupid + " and Brandid = " + Brandid;
using (DataSet MySet = DbHelperSQL.Query(strsql))
{
if (MySet.Tables[0].Rows.Count == 0)
{
lBoxNew.Items.Add(item);
string BGName = ddlNewinfo.SelectedItem.Text;
string sql = "insert into BranbGroup_Relation (BGroupid,Brandid) values (" + BGnewroupid + "," + Brandid + ")";
int iResult = DbHelperSQL.ExecuteSql(sql);
if (iResult == 1)
{
continue;
}
}
}
}
catch (Exception ex)
{
scriptinfo.InnerHtml = "<script>alert('" + ex.Message + "')</script>";
return;
}
}
}
else
{
scriptinfo.InnerHtml = "<script>alert('左侧品牌组中没有品牌信息!')</script>";
return;
}
}
//删除右侧选中品牌数据
protected void btnDelete_Click(object sender, EventArgs e)
{
scriptinfo.InnerHtml = "";
if (lBoxNew.Items.Count > 0)
{
int iselectcount = 0;
for (int j = 0; j < lBoxNew.Items.Count; j++)
{
if (lBoxNew.Items[j].Selected == true)
{
iselectcount++;
}
}
if (iselectcount == 0)
{
scriptinfo.InnerHtml = "<script>alert('请选择需要删除的品牌!')</script>";
return;
}
string strBGid = ddlNewinfo.SelectedValue;
string Brandid = "";
int count = lBoxNew.Items.Count;
int index = 0;
for (int i = 0; i < count; i++)
{
ListItem item = lBoxNew.Items[index];
if (lBoxNew.Items[index].Selected == true) //判断当前列表框中选择的列表项
{
try
{
Brandid = lBoxNew.Items[index].Value;
string sql = "delete from BranbGroup_Relation where BGroupid = " + strBGid + " and Brandid = " + Brandid;
int iResult = DbHelperSQL.ExecuteSql(sql);
if (iResult == 1)
{
lBoxNew.Items.Remove(item); //移除当前列表框中选择的列表项
index--;
}
}
catch (Exception ex)
{
scriptinfo.InnerHtml = "<script>alert('" + ex.Message + "')</script>";
return;
}
}
index++;
}
}
else
{
//无数据
scriptinfo.InnerHtml = "<script>alert('品牌组中没有品牌信息!')</script>";
return;
}
}
//删除右侧全部品牌数据
protected void btnDeleteAll_Click(object sender, EventArgs e)
{
scriptinfo.InnerHtml = "";
if (lBoxNew.Items.Count > 0)
{
string strBGid = ddlNewinfo.SelectedValue;
string Brandid = "";
int count = lBoxNew.Items.Count;
int index = 0;
for (int i = 0; i < count; i++)
{
ListItem item = lBoxNew.Items[index];
try
{
Brandid = lBoxNew.Items[index].Value;
string sql = "delete from BranbGroup_Relation where BGroupid = " + strBGid + " and Brandid = " + Brandid;
int iResult = DbHelperSQL.ExecuteSql(sql);
if (iResult == 1)
{
lBoxNew.Items.Remove(item); //移除当前列表框中选择的列表项
index--;
}
}
catch (Exception ex)
{
scriptinfo.InnerHtml = "<script>alert('" + ex.Message + "')</script>";
return;
}
index++;
}
}
else
{
//无数据
scriptinfo.InnerHtml = "<script>alert('品牌组中没有品牌信息!')</script>";
return;
}
}
另外一种ListBox获取并移除选中项目的方法:
while (ListBox1.SelectedIndex != -1)
{
ListBox1.Items.Remove(ListBox1.SelectedItem);
}