首页 新闻 赞助 找找看

GridView中实现CheckBox的全选

0
[已解决问题] 解决于 2012-03-19 11:57
 1         function checkFormAll(chk)
2 {
3 form = document.getElementById("CheckBoxAll");
4 for(var i=0; i<form1.elements.length; i++)
5 {
6 if (form1.elements[i].type=="checkbox")
7 {
8 form1.elements[i].checked = chk;
9 }
10 }
11 }
1 <asp:TemplateField>
2 <HeaderTemplate>
3 <input type="checkbox" id="CheckBoxAll" onclick="checkFormAll(this.checked)" runat="server"/>
4 </HeaderTemplate>
5 <ItemStyle Width="5%"/>
6 <ItemTemplate>
7 <asp:CheckBox ID="CheckItem" runat="server"/>
8 </ItemTemplate>
9 </asp:TemplateField>
 protected void LinkButton_DeleteAll_Click(object sender, EventArgs e)
{

for (int i = 0; i < this.GridContent1.Rows.Count; i++)
{
CheckBox cBoxItem = (CheckBox)GridContent1.Rows[i].FindControl("CheckItem");
if (cBoxItem.Checked == true)
{
Label Label_cBoxItem = (Label)GridContent1.Rows[i].FindControl("Label_CBoxItem");
string info_id = Label_cBoxItem.Text;

DataSet ds = editorContent.GetEditorContent(info_id);
DataRow row = ds.Tables[0].Rows[0];

string title = row["title"].ToString();
string keyword = row["keyword"].ToString();
string source = row["source"].ToString();
string sourceUrl = row["source_url"].ToString();
string Abstract = row["ABSTRACT"].ToString();
string Content = row["content"].ToString();
string LastMod_UserId = row["LastMod_UserId"].ToString();
string Res_filenum = row["Res_filenum"].ToString();
string Cat_id = row["Cat_id"].ToString();
string Info_filename = row["Info_filename"].ToString();
string Info_ABS_Path = row["Info_ABS_Path"].ToString();
string Info_Rel_Path = row["Info_Rel_Path"].ToString();
string Full_Path = row["Full_Path"].ToString();
string Option_Type = "DEL";

bool InsertHisEditor1 = editorContent.InsertHisEditor(info_id, Cat_id, Info_filename, Info_ABS_Path, Info_Rel_Path, title, keyword, Abstract, Full_Path, source, sourceUrl, Res_filenum, Option_Type, Content, LastMod_UserId); //插入删除记录
if (i==GridContent1.Rows.Count-1)
{
Response.Write("<script>alert('删除成功!');window.location.href='Kdb_Manage_Main.aspx?catid=" + Cat_id + "';</script>");
}
}
}
}

为什么上面这几段代码执行后没有删除gridview中选择的数据?cBoxItem.Checked 一直显示等于false


Mr.ch的主页 Mr.ch | 初学一级 | 园豆:6
提问于:2012-03-15 16:48
< >
分享
最佳答案
0

你全选、全不选的时候页面上面有没有显示下面的子checkbox的选中状态。

如果显示了的话,就是其他地方又将这个属性修改过来。

否则就是前面的脚本执行不成功,或者没有执行!

小小刀 | 小虾三级 |园豆:1991 | 2012-03-15 19:45

谢谢。最后发现是页面加载时那个ispostback那里的逻辑出错,改过了就可以了。

Mr.ch | 园豆:6 (初学一级) | 2012-03-19 11:56
其他回答(3)
0

我可以试试这个全先脚本:

function checkAll() {
    if ($(":checkbox")[1]["checked"]) {
        $(":checkbox").attr("checked", false);
    }
    else {
        $(":checkbox").attr("checked", true);
    }
}

你在实例对象 CheckBox cBoxItem 时,最好先判断一下,cBoxItem是否为空,这样不容易报错;

InsertHisEditor()方法是插入数据的还是删除数据的,你的删除数据方法在什么地方,在这个方法里吗?

在判断是否提示删除成功时,判断条件不对,上面方法执行完后,页面还没有刷新,GridView中的数据行不会变;

我觉得删除成功提示不应该放在for循环里面,建议写两个变量来保存删除成功与删除失败的个数,通过删除方法返回的结果给这两个变量赋值,在完成所有删除数据后,给一个总的提示。。。

KivenRo | 园豆:1734 (小虾三级) | 2012-03-16 09:06
0

CheckBoxAll這個被選中了CheckItem沒有選中。
      function checkFormAll(chk)
2 {
3 form = document.getElementById("CheckItem");
4 for(var i=0; i<form1.elements.length; i++)
5 {
6 if chk.checked =="checkbox")
7 {
8 form1.elements[i].checked = chk;
9 }
10 }
試一下。把js改成 上面的哦
無限遐想 | 园豆:3740 (老鸟四级) | 2012-03-16 09:10
0

function GetAllCheckedBox(CheckAll) {
var items = document.getElementsByTagName("input"); //获得所有input对象的列表,并遍历该列表
for (i = 0; i < items.length; i++) {
if (items[i].type == "checkbox") {
items[i].checked = CheckAll.checked;
}
}
}

秋风sao落叶 | 园豆:44 (初学一级) | 2012-03-16 21:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册