有一个datagrid模板列,每条记录第一列有一个checkbox,可以多选,点击button可以将选择的记录删除,如何实现
难点:1。如何保存checkde那行的索引
2。知了索引如何一起删除欢迎大家讨论,UP有分
难点:1。如何保存checkde那行的索引
2。知了索引如何一起删除欢迎大家讨论,UP有分
解决方案 »
- 怎样判断表名日期模糊查询
- asp.net菜鸟问题
- asp.net小问题
- 打开VS后->website->asp.net configuration就出现这样的错误提示,在应用程序配置中未找到连接名LocalSqlServer或者连接字符串为空
- 关于.net1.1中如何通过页面来修改web.config中的参数
- 请各位帮我看一下这个错误是什么问题?
- System.Web.HttpContext.Current.Server.Execute的问题
- DataSet绑定DataGrid的问题,在线等
- 抽象类问题
- [算法] 概率分组
- DropDownList下拉列表框怎么能实现每一个选项用不同的颜色的值来显示?
- 树形控件的搜索问题
加一列记录信息的ID
还有一行是Checkbox用来选中的
然后在删除处理方法中获取
int count=FileListDG.Items.Count;
for(int i=0;i<count;i++)
{
if(((CheckBox)(FileListDG.Items[i].FindControl("CheckBox1"))).Checked)
{
int selectFileID=Int32.Parse(FileListDG.Items[i].Cells[0].Text.ToString().Trim());
//处理代码
}
}
比如说隐藏在第一列
2:string id = "";
foreach(DataGridItem dgi in yourGrid.Items)
{
CheckBox cb = (CheckBox)dgi.FindControl("yourCheckBoxId");
if(cb.Checked)
{
id = dgi.Items.Cell[0].Text + ",";
}
}if(id.Length > 0)
{
id = "(" + id.Substring(0,id.Length - 1) + ")";
string sql = "delete from yourtable where id in " + id;
//执行这个sql就可以拉
}
private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemIndex ==-1)
return;
//控制选择的情况
HtmlInputCheckBox chk = (HtmlInputCheckBox)e.Item.Cells[0].FindControl("CheckBox2");
if(chk!=null)
{
chk.ID="chk_"+DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
chk.Attributes["onclick"]=" return changeit(this);";
}
}//然后脚本中判断选择的信息
function checkAll(){
var select = document.getElementById("chk_All");
var IsSelected = select.checked;
var all = new Array();
all= document.getElementsByTagName("input");
for(i = 0;i<all.length;i++){
if(all[i].type.toLowerCase() =="checkbox"){
all[i].checked = IsSelected;
if((all[i].id!=select.id) && all[i].checked){
SaveInfo(all[i].id);
}
if(!all[i].checked){
ChangeSaveInfo(all[i].id);
}
}
}
}
//保存信息
function SaveInfo(obj){
var txt=document.getElementById("Text_Save");
if(txt.value .indexOf(obj)==-1)
txt.value =obj+";"+txt.value;
}
function ChangeSaveInfo(obj){
var txt=document.getElementById("Text_Save");
var saved =txt.value;
if(saved.indexOf(obj)!=-1){
saved =saved.replace(obj+";","");
}
txt.value =saved;
}
function changeit(e){
if(e.checked){
SaveInfo(e.id);
}else{
ChangeSaveInfo(e.id);
}
}//然后接收Text_Save.Text中的值就是选择的控件的索引了
//最后 在服务器端进行分析Text_Save.Text的值,实现删除功能
Dim dgItem As DataGridItem = dgReceive.Items(i)
Dim chkBox As CheckBox = dgItem.FindControl("chkSubmit")'定义摸板列中的textbox If chkBox.Checked = True Then'如果被选中
调用删除该行
End If
Next注意:::page_load 中 datagrid一定要包含在 If Not Page.IsPostBack = True then ........end 中
dim str as string
For i = 0 To dgReceive.Items.Count - 1‘遍历datagrid
Dim dgItem As DataGridItem = dgReceive.Items(i)
Dim chkBox As CheckBox = dgItem.FindControl("chkSubmit")'定义摸板列中的textbox If chkBox.Checked = True Then'如果被选中
str=该行的ID,’一定要加逗号!
调用方法以 str为参数
End If
Next
存储过程中可以这样写。
if id 中包含逗号
delete table where id='id'
else
delete from table where id in (str)
保存状态的方法
/// <summary>
/// 方法编号:10
/// 方法名称:GetSelectID
/// 内容摘要:获取选中的记录集合
/// </summary>
/// <param name="dg">当前操作的DataGrid控件</param>
private void GetSelectID(DataGrid dg)
{
//创建集合对象
ArrayList aryLstID = null;
if (!object.Equals(ViewState["aryLstID"],null))
{
aryLstID = ViewState["aryLstID"] as ArrayList;
}
else
{
aryLstID = new ArrayList();
}
//创建Hashtable对象,用于存放所选毕业生的姓名和学历
Hashtable htXueLi = null;
if (!object.Equals(ViewState["htXueLi"],null))
{
htXueLi = ViewState["htXueLi"] as Hashtable;
}
else
{
htXueLi = new Hashtable();
} foreach(DataGridItem dgItem in dg.Items)
{
CheckBox chk = dgItem.FindControl("chkSelect") as CheckBox; if (chk != null)
{
//如果此CheckBox选中,并且集合中没有此项,就将此项添加到集合中
if (chk.Checked && !aryLstID.Contains(dg.DataKeys[dgItem.ItemIndex]))
{
aryLstID.Add(dg.DataKeys[dgItem.ItemIndex]);
}
//如果没有选中,但集合中有此项,就将此项删除
else if(!chk.Checked && aryLstID.Contains(dg.DataKeys[dgItem.ItemIndex]))
{
aryLstID.Remove(dg.DataKeys[dgItem.ItemIndex]);
}
//将毕业生学历,姓名,ID号添加到Hashtable中
if (chk.Checked && !htXueLi.ContainsKey(dgItem.Cells[1].Text + "-" + dgItem.Cells[2].Text))
{
htXueLi.Add(dgItem.Cells[1].Text + "-" + dgItem.Cells[2].Text,dgItem.Cells[5].Text);
}
//将毕业生学历,姓名,ID号从Hashtable中删除
else if (!chk.Checked && htXueLi.ContainsKey(dgItem.Cells[1].Text + "-" + dgItem.Cells[2].Text))
{
htXueLi.Remove(dgItem.Cells[1].Text + "-" + dgItem.Cells[2].Text);
}
}
} ViewState["aryLstID"] = aryLstID;
ViewState["htXueLi"] = htXueLi; }
/// <summary>
/// 方法编号:09
/// 方法名称:dgList_PageIndexChanged
/// 内容摘要:DataGrid分页事件
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private void dgList_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//添加选中的项到集合对象中
this.GetSelectID((DataGrid)source); //分页
((DataGrid)source).CurrentPageIndex = e.NewPageIndex;
this.btnQuery_ServerClick(null,null);
}
/// <summary>
/// 方法编号:08
/// 方法名称:dgList_ItemDataBound
/// 内容摘要:DataGrid绑定事件,用于产生序列号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgList_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//在第一列中增加序列号列
e.Item.Cells[0].Text = Convert.ToString(e.Item.ItemIndex + 1); //如果已经存在集合对象
if (!object.Equals(ViewState["aryLstID"],null))
{
ArrayList aryLstID = ViewState["aryLstID"] as ArrayList;
//判断集合对象中是否有此记录项,如果有就选中
if (aryLstID != null && aryLstID.Contains(this.dgList.DataKeys[e.Item.ItemIndex]))
{
CheckBox chk = e.Item.FindControl("chkSelect") as CheckBox; if (chk != null)
{
chk.Checked = true;
intCount += 1;
}
}
}
}
}