Js實現全選和批量刪除


<asp:CheckBox ID="chbAll" runat="server" Text="全選" onclick="GetAll(this)"/>

<script language="javascript" type="text/javascript">

      function GetAll(obj)

{

var item=document.gerElementByTagName("input");   //他獲取的東西是一個集合,所以可以用for循環

for(i=0;i<item.length;i++)

{

if(item[i].type=="checkbox")

{

item[i].checked=obj.checked;

}

}

}

<asp:CheckBox ID="chbSelect" runat="server" />

<asp:HiddenField runat="server" ID="hfId" Value='<%#Eval("Id") %>' />// 通過隱藏域來保存每一條需要刪除的ID

上面的js代碼僅僅是選擇了一個頁面的所有checkbox;

下面的cs代碼則是實現批量刪除:每一個checkbox和隱藏域是放在repeat中的

 protected void btnDelSelect_Click(object sender, EventArgs e)
{

             int count = 0;
            string ch = string.Empty;//存放選擇的ID
            string jch = string.Empty;//截取后的ID

             foreach(RepeaterItem ri in repAll.Items)

              {

                 CheckBox cb=ri.FindControl("chbSelect") as CheckBox;

                 HiddenField hid=ri.FindControl("hfid") as HiddenField;

                 if(cb.Checked==true)

               {

                        count++;

                         ch+=hid.Value.ToString()+",";

                         jch=ch.Substring(0,ch.LastIndexOf(','));

               }

           if (count > 0)
            {
                bool flag = new VideosBLL().DeleteVideoMember(jch);
                if (flag)
                {
                    PageHelper.ShowMsgBox(this, "刪除成功!", PageHelper.MsgStatus.succeed, 1);
                    GetDocumentUpLoad();
                }
                else
                {
                    PageHelper.ShowMsgBox(this, "刪除失敗!");
                }
            }
            if (count == 0)
            {
                PageHelper.ShowMsgBox(this, "請選擇您要刪除的數據!", PageHelper.MsgStatus.question, 0);
            }

}

}

        /// <summary>
        /// 批量刪除用戶
        /// </summary>
        /// <param name="ids">用戶Ids</param>
        /// <returns></returns>
        public bool DeleteVideoMember(string id)
        {
            string strWhere = "Id in(" + id + ")";
            bool flag = GetPagedRecord.ModifyData("Videos", "IsDelete", "1", strWhere, 1);
            return flag;
        }

        /// <param name="tbName">表名</param>
        /// <param name="modifyField">字段名</param>
        /// <param name="modifyData">字段值</param>
        /// <param name="strWhere">where條件,不加where</param>
        /// <param name="isUpdateTime">是否更新時間(須有updateTime字段才可以)</param>
        /// <returns></returns>
        public static bool ModifyData(string tbName, string modifyField, string modifyData, string strWhere, int isUpdateTime)
        {
            bool flag = false;

            SqlParameter[] paras = new SqlParameter[] {  
                    new SqlParameter("@modifyData",SqlDbType.NVarChar,2000)                 
                };
            paras[0].Value = modifyData;
            string sql = "update " + tbName + " set " + modifyField + "=@modifyData " + (isUpdateTime == 1 ? " ,UpdateTime=getdate()" : "") + (string.IsNullOrEmpty(strWhere) ? "" : " where " + strWhere);
            int result = SqlHelper.ExecuteNonQuery(SqlHelper.MainConnectionString, CommandType.Text, sql, paras);
            if (result > 0)
            {
                flag = true;
            }
            return flag;
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM