GridView控制項實現大量刪除功能(checkbox)

來源:互聯網
上載者:User

 分頁檔:

 
    // 多選的全選與取消
    function checkJs(boolvalue)
    {
        if(document.all.checkboxname.length>1)
        {
            for(var i=0;i<document.all.checkboxname.length;i++)
            {
                document.all.checkboxname[i].checked = boolvalue;           
            }
        }
        else
            document.all.checkboxname.checked = boolvalue;
    }
    //
    // 只有全部選中時“全選”選中
    function SingleCheckJs()
    {
        var flag1=false;
        var flag2=false;
       
        if (document.all.checkboxname.length)
        {
        //
            for (var i=0;i<document.all.checkboxname.length;i++)
            {
                if(document.all.checkboxname[i].checked)
                    flag1 = true;
                else
                    flag2 = true;
            }
        }
        else
        {
            if(document.all.checkboxname.checked)
                flag1 = true;
            else
                flag2 = true;
        }
        //
        if(flag1==true&&flag2==false)
            document.getElementById("chk").checked = true;
        else
            document.getElementById("chk").checked = false;
    }

 

 <asp:GridView ID="GridView1" runat="server" Height="276px" Width="651px" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" ShowFooter="True" OnSorting="GridView1_Sorting">
         <Columns>
             <asp:TemplateField HeaderText="&lt;input type='checkbox' id='chk' name='chk' onclick='checkJs(this.checked);'  /&gt;全選">
                                    <ItemTemplate>
                                        <input type="checkbox" id="checkboxname" name="checkboxname" value='<%# DataBinder.Eval(Container.DataItem, "記錄ID")%>' onclick='SingleCheckJs();' />
                                    </ItemTemplate>
                                </asp:TemplateField>
             <asp:TemplateField HeaderText="自增列">
                                    <ItemTemplate>
                                        <%# (Container.DataItemIndex+1).ToString()%>
                                    </ItemTemplate>
              </asp:TemplateField>
             <asp:BoundField DataField="月記錄ID" HeaderText="月記錄編號"  />
             <asp:BoundField DataField="機型名" HeaderText="機型名"  />
             <asp:BoundField DataField="客戶名" HeaderText="客戶名"  />
             <asp:BoundField DataField="發貨時間" HeaderText="發貨時間" DataFormatString="{0:yyyy年MM月dd日}"  SortExpression="發貨時間" />
             <asp:TemplateField   HeaderText='出貨數量<img src="Images/up.gif"  onclick="reserver();" visible="true"  width="14" height="8" id="imgNavigator" name="imgNavigator"/>'  SortExpression="出貨數量">
              <ItemTemplate > <asp:Label ID="lbNum" runat="server" Text='<%# AdddieHundertstelPoint(Eval("出貨數量").ToString())%>'></asp:Label> </ItemTemplate>
             </asp:TemplateField>
             <asp:BoundField DataField="出貨倉庫" HeaderText="出貨倉庫"  ReadOnly="True" />
         </Columns>
         <RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
         <HeaderStyle BackColor="Silver" Font-Bold="True" Font-Names="Arial" />
         <AlternatingRowStyle BackColor="#E0E0E0" />
         <PagerSettings Visible="False" />
     </asp:GridView>

 

 

 後台代碼:

   /// <summary>
    /// 大量刪除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btDelete_Click(object sender, EventArgs e)
    {
        string str = "";
        string[] ckb = null;

        str = Request.Form.Get("checkboxname");
        ckb = str.Split(new char[] { ',' });
            SqlParameter[] parms = {new SqlParameter(Parm_CustoemrGoods_ID,SqlDbType.Int)
                               };

         
        using (SqlConnection conn= new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))//開始事務
            { 
            conn.Open();
            SqlCommand comm = conn.CreateCommand();
                comm.Connection = conn;
                comm.CommandType = CommandType.StoredProcedure;
                comm.CommandText = Pro_CustomerGoods_uCustomerGoods_SerialDeleteRecord;
            SqlTransaction DeleteMachineTran = conn.BeginTransaction();
            comm.Transaction = DeleteMachineTran; 
         
        for (int i = 0; i < ckb.Length; i++)
        {
            comm.Parameters.Clear();
            foreach (SqlParameter parm in parms)
            {
                comm.Parameters.Add(parm);
            }
            parms[0].Value = int.Parse(ckb[i]);//id號;
            //記錄ID;             
                try
                {
                    if (comm.ExecuteNonQuery() <= 0)
                    {
                        DeleteMachineTran.Rollback();
                        Response.Write("<script language='javascript'>alert('刪除失敗!')</script>");
                        return;
                    }
                }
                catch (SqlException ex)
                {
                    DeleteMachineTran.Rollback();
                    ProjectERP.Exception.DealException.FillMessageFile("/n錯誤碼:" + ex.Number + "/n錯誤發生的時間:" + DateTime.Now + "/n錯誤資訊" + ex.Message + "/n發生錯誤的儲存;" + ex.Procedure + "/n發生當前異常的方法是:" + ex.TargetSite + "/n" + ex.ToString());
                    return;
                }
                catch (System.Exception eb)
                {
                    DeleteMachineTran.Rollback();
                    ProjectERP.Exception.DealException.FillMessageFile("錯誤發生的時間:" + DateTime.Now + "/n錯誤資訊" + eb.Message + "/n發生錯誤的儲存;" + "/n發生當前異常的方法是:" + eb.TargetSite + "/n" + eb.ToString());
                    return;
                }

          
        }
        Response.Write("<script language='javascript'>alert('刪除資料成功')</script>");
        DataTable result = null;//資料來源;
        if (this.Cache["SerialCustomerGoods"] != null)
            result = this.Cache["SerialCustomerGoods"] as DataTable;
        else
        {
            result = this.Session["SerialCustomerGoods"] as DataTable;
        }

       for(int j=0;j< result.Rows.Count;j++)
        {
            for (int i = 0; i < ckb.Length; i++)
            {
                if (int.Parse(result.Rows[j]["記錄ID"].ToString()) == int.Parse(ckb[i]))//刪除此記錄;
                {
                    result.Rows.Remove(result.Rows[j]);
                    break;
                }

            }
        }

        //把結果表存入到Cache中和session中;
        this.Cache.Insert("SerialCustomerGoods", result, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromHours(12.0), System.Web.Caching.CacheItemPriority.High, null);//把資料存入cache中
        this.Session.Add("SerialCustomerGoods", result);//Session此資料區塊;

        this.GridView1.DataSource = result;
        this.GridView1.DataBind();
 

     }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.