CheckBox和GridView

来源:互联网 发布:mac怎么打开cr2图片 编辑:程序博客网 时间:2024/05/17 23:07
 
create table Paging(PagingId int primary key,PagingName varchar(20),PagingContent varchar(20))declare @a intset @a=1while(@a<=30)begindeclare @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+1continueend
<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"         onclick="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>");        }            }

原创粉丝点击