GRIDVIEW第一列是CHECKBOX,
先勾上,那么把这条数据的主键ID和第二列的数据让二维数组保存。
然后取消勾上,那有两个问题我没有解决:
1:怎么比较这条未勾上的数据的主键ID和第二列的值在二维数组里?
2:然后怎么把这条未勾上的数据的主键ID和第二列的数据从二维数组里删除掉?struct Data
{
public int id;
public string name;
}private void RememberOldValues()
{
ArrayList categoryIDList = new ArrayList();
Data d = new Data(); int index = -1;
string kkk = ""; foreach (GridViewRow row in GridView1.Rows)
{
index = (int)GridView1.DataKeys[row.RowIndex].Value;
kkk = row.Cells[1].Text; bool result = ((CheckBox)row.FindControl("CheckBox1")).Checked; if (Session["CHECKED_ITEMS"] != null)
{
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
} if (result) //这里表示以选上
{
if (!categoryIDList.Contains(index))
1:怎么比较这条未勾上的数据的主键ID和第二列的值在二维数组里?
{
d.id = index;
d.name = kkk;
categoryIDList.Add(d);
}
}
else
{
//d.id = index;
//d.name = kkk;
//categoryIDList.Remove(index);
//categoryIDList.Remove(d.id);
//categoryIDList.Remove(d.name);
//categoryIDList.Remove(d);
2:然后怎么把这条未勾上的数据的主键ID和第二列的数据从二维数组里删除掉?
}
} if (categoryIDList != null && categoryIDList.Count > 0)
{
Session["CHECKED_ITEMS"] = categoryIDList;
}
} private void RePopulateValues()
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
foreach (GridViewRow row in GridView1.Rows)
{
int index = (int)GridView1.DataKeys[row.RowIndex].Value;
if (categoryIDList.Contains(index))
{
CheckBox myCheckBox = (CheckBox)row.FindControl("CheckBox1");
myCheckBox.Checked = true;
}
}
}
} protected void ItemCheckBox_CheckedChanged(object sender, EventArgs e)
{
RememberOldValues();
RePopulateValues();
string selectName = "";
if (Session["CHECKED_ITEMS"] != null && Session["CHECKED_ITEMS"].ToString() != "")
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"]; if (categoryIDList != null && categoryIDList.Count > 0)
{
for (int i = 0; i < categoryIDList.Count; i++)
{
Data d = (Data)categoryIDList[i];
selectName = selectName + "<a href='#' onclick=del('" + d.name + "') runat='server'>" + d.name + "</a>";
Label1.Text = selectName;
}
}
else
{
Label1.Text = "";
}
}
}
先勾上,那么把这条数据的主键ID和第二列的数据让二维数组保存。
然后取消勾上,那有两个问题我没有解决:
1:怎么比较这条未勾上的数据的主键ID和第二列的值在二维数组里?
2:然后怎么把这条未勾上的数据的主键ID和第二列的数据从二维数组里删除掉?struct Data
{
public int id;
public string name;
}private void RememberOldValues()
{
ArrayList categoryIDList = new ArrayList();
Data d = new Data(); int index = -1;
string kkk = ""; foreach (GridViewRow row in GridView1.Rows)
{
index = (int)GridView1.DataKeys[row.RowIndex].Value;
kkk = row.Cells[1].Text; bool result = ((CheckBox)row.FindControl("CheckBox1")).Checked; if (Session["CHECKED_ITEMS"] != null)
{
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
} if (result) //这里表示以选上
{
if (!categoryIDList.Contains(index))
1:怎么比较这条未勾上的数据的主键ID和第二列的值在二维数组里?
{
d.id = index;
d.name = kkk;
categoryIDList.Add(d);
}
}
else
{
//d.id = index;
//d.name = kkk;
//categoryIDList.Remove(index);
//categoryIDList.Remove(d.id);
//categoryIDList.Remove(d.name);
//categoryIDList.Remove(d);
2:然后怎么把这条未勾上的数据的主键ID和第二列的数据从二维数组里删除掉?
}
} if (categoryIDList != null && categoryIDList.Count > 0)
{
Session["CHECKED_ITEMS"] = categoryIDList;
}
} private void RePopulateValues()
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
foreach (GridViewRow row in GridView1.Rows)
{
int index = (int)GridView1.DataKeys[row.RowIndex].Value;
if (categoryIDList.Contains(index))
{
CheckBox myCheckBox = (CheckBox)row.FindControl("CheckBox1");
myCheckBox.Checked = true;
}
}
}
} protected void ItemCheckBox_CheckedChanged(object sender, EventArgs e)
{
RememberOldValues();
RePopulateValues();
string selectName = "";
if (Session["CHECKED_ITEMS"] != null && Session["CHECKED_ITEMS"].ToString() != "")
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"]; if (categoryIDList != null && categoryIDList.Count > 0)
{
for (int i = 0; i < categoryIDList.Count; i++)
{
Data d = (Data)categoryIDList[i];
selectName = selectName + "<a href='#' onclick=del('" + d.name + "') runat='server'>" + d.name + "</a>";
Label1.Text = selectName;
}
}
else
{
Label1.Text = "";
}
}
}
解决方案 »
- asp.net怎么导入Excel到数据库的,急
- hibernate中有复合主键,且其中有一个有外键的查询方法
- asp.net 读取Excel内图片
- 刚学有点疑问,请看注释
- asp.net用户控件取值问题 高手进
- “System.Web.Profile.SqlProfileProvider”要求一个与架构版本“1”兼容的数据库架构
- 请教安装vs.net2005的问题?
- 关于如何在.vb或web.config文件中获得应用程序在磁盘上的物理路径?
- 一个小问题
- <%Response.Write Request.QueryString("id")%>为什么错误?
- web页面调用mapxinfo问题
- 用Javascript 获取asp.net DataList模板中控件值的问题
查询Dictionary实现删除
foreach(string key in Dit.Keys)
{
}
{
//存在
} dic.Remove(id);
Dit = (Dictionary)Session["CHECKED_ITEMS"];Error 2 Using the generic type 'System.Collections.Generic.Dictionary<TKey,TValue>' requires '2' type arguments
ArrayList? 你的不是键值对么?
先勾上,那么把这条数据的主键ID和第二列的数据让二维数组保存。
然后取消勾上,那有两个问题我没有解决:
1:怎么比较这条未勾上的数据的主键ID和第二列的值在二维数组里?
2:然后怎么把这条未勾上的数据的主键ID和第二列的数据从二维数组里删除掉?思路:
先勾上,那么把这条数据的主键ID和第二列的数据存储在Dictionary中(逐条添加 使用 dic.Add(key,value))
取消勾上,把这条未勾上的数据的主键ID和第二列的数据从二维数组里删除掉(使用 dic.Remove(key) )Dictionary<int, string> dic = new Dictionary<int, string>();
//....添加 ID 和第二列值 (键值对)
if (dic.Keys.Contains<int>(id)) //判断是否存在某ID
{
//存在
} dic.Remove(id); //删掉某键值对
for(int i=0;i<list.count;i++){
list.Remove(list[0]);
}
也可以用HashTable
{
index = (int)GridView1.DataKeys[row.RowIndex].Value;
kkk = row.Cells[1].Text;fruit.Add(index, kkk);if (fruit != null && fruit.Count > 0)
{
Session["CHECKED_ITEMS"] = fruit;
}
foreach (GridViewRow row in GridView1.Rows)
{
index = (int)GridView1.DataKeys[row.RowIndex].Value;
kkk = row.Cells[1].Text; fruit.Add(index, kkk);
} //这是不是有个 } 啊?
if (fruit != null && fruit.Keys.Count > 0)
{
Session["CHECKED_ITEMS"] = fruit;
}