CheckBox和GridView

 
create table Paging
(
	PagingId int primary key,
	PagingName varchar(20),
	PagingContent varchar(20)
)

declare @a int
set @a=1
while(@a<=30)
begin
declare @b varchar(10)
declare @c varchar(10)
set @b='Name'+convert(varchar(3),@a)
set @c='Content'+convert(varchar(3),@a)
insert into Paging values(@a,@b,@c)
set @a=@a+1
continue
end
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        AllowPaging="true" onpageindexchanging="GridView1_PageIndexChanging">
    <Columns>
    <asp:TemplateField>
    <HeaderTemplate>
    <input type="checkbox" id="chkAll" />
    </HeaderTemplate>
    <ItemTemplate>
    <input id="chkItem" type="checkbox" value='<%#Eval("PagingId") %>' runat="server"/>
    </ItemTemplate>
    </asp:TemplateField>
        <asp:BoundField DataField="PagingId" HeaderText="编号" />
        <asp:BoundField DataField="PagingName" HeaderText="名字" />
        <asp:BoundField DataField="PagingContent" HeaderText="内容" />
    </Columns>
    </asp:GridView>
    <input type="button" id="btnClient" value="客户端获取选择记录"/>   
    <asp:Button ID="btnServer" runat="server" Text="btnServer" 
        οnclick="btnServer_Click" />
    <script src="JS/jquery-1.3.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#chkAll").click(function () {
                $('#<%=GridView1.ClientID %>>tbody>tr>td>input:checkbox').attr('checked', this.checked);
            });
            $('#<%=GridView1.ClientID %>>tbody>tr>td>input:checkbox').click(function () {
                var expr1 = '#<%=GridView1.ClientID%>>tbody>tr>td>input:checkbox:checked';
                var expr2 = '#<%=GridView1.ClientID %> >tbody >tr >td >input:checkbox';
                var selectAll = $(expr1).length == $(expr2).length;
                $('#chkAll').attr('checked', selectAll);
            });
            $("#btnClient").click(function () {
                var chkList = $("input:checkbox:checked[name$=chkItem]");
                var arrayList = new Array();
                var j = 0;
                for (var i = 0; i < chkList.length; i++) {
                    arrayList.push(chkList[i].value);
                    j++;
                }
                if (arrayList.length > 0) {

                    confirm("确定要删除吗?")
                    {
                        GridViewCheckAll._Default.Delete(arrayList.slice(0, j));
                        $('input:checkbox:checked').parent().parent().remove();
                    }     
                }
                else {
                    alert("请选择记录");
                }
            });
        })
    </script>
</asp:Content>


   public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
            if (!IsPostBack)
            {
                data_bind();
            }
        }
      
        [AjaxPro.AjaxMethod]
        public void data_bind()
        {
            this.GridView1.DataSource = Bind();
            this.GridView1.DataBind();
        }
        [AjaxPro.AjaxMethod]
        public DataTable Bind()
        {
            SqlConnection conn = new SqlConnection("server=.;database=Example;uid=sa;pwd=**;");
            DataTable dt = new DataTable();
            string sql = "select * from Paging";
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            da.Fill(dt);
            return dt;
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            data_bind();
        }
        [AjaxPro.AjaxMethod]
        public bool Delete(int[] m)
        {
            string[] sqls = new string[m.Length];
            int i = 0;
            foreach (int id in m)
            {
                sqls[i] = string.Format("delete from Paging where PagingId={0}", id);
                i++;
            }
            return DeleteBooksById(sqls);    
        }
        public bool DeleteBooksById(string[] sqls)
        {
            bool flag = true;
            SqlConnection conn = new SqlConnection("server=.;database=Example;uid=sa;pwd=**;");
            try
            {
                conn.Open();
                SqlTransaction tran = conn.BeginTransaction();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.Transaction = tran;
                for (int i = 0; i < sqls.Length; i++)
                {
                    cmd.CommandText = sqls[i];
                    if (cmd.ExecuteNonQuery() == 0)
                    {
                        flag = false;
                        tran.Rollback();
                        return flag;
                    }
                }
                flag = true;
                tran.Commit();
                return flag;

            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                conn.Close();
            }
        }

        protected void btnServer_Click(object sender, EventArgs e)
        {
            string s = string.Empty;
            if (GridView1.Rows.Count > 0)
            {
                foreach (GridViewRow row in GridView1.Rows)
                {
                    HtmlInputCheckBox chkItem = (HtmlInputCheckBox)row.FindControl("chkItem");
                    if (chkItem != null && chkItem.Checked)
                    {
                        s += chkItem.Value + ",";
                    }
                }
            
            }
            Response.Write("<script>alert('"+s+"')</script>");
        }
        
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值