现在的位置: 首页 > 综合 > 正文

自己写的一个GridView的JS全选及删除选定

2013年10月25日 ⁄ 综合 ⁄ 共 1459字 ⁄ 字号 评论关闭

js文件

function checkFormAll(chk)
{
    form = document.getElementById("select_all");
    for(var i=0; i<form1.elements.length; i++)
    {
        if (form1.elements[i].type=="checkbox")
        {
         form1.elements[i].checked = chk;
        }
    }
}

 

GridView里         <Columns>里粘贴下面的代码

                            <asp:TemplateField>
                                <HeaderTemplate>
                                    <input id="select_all" onclick="checkFormAll(this.checked)" type="checkbox">
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="select_one" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>

 

全选删除代码

    protected void Button1_Click(object sender, EventArgs e)
    {
        string str_id = "";
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("select_one");
            if (cbox.Checked == true)
            {
                string id = GridView1.DataKeys[i].Value.ToString();
                if (i == this.GridView1.Rows.Count - 1)
                {
                    str_id = str_id + id;
                }
                else
                {

                    str_id = str_id + id + ",";
                }
            }
        }
        if (str_id == "")
        {
            StringCheck.Show(this.Page, "没有选定内容!");
            return;
        }
        string strSql1 = "delete news where id in(" + str_id + ")";

执行sql语句

绑定
    }

【上篇】
【下篇】

抱歉!评论已关闭.