Tạo chức năng multiDelete giống Yahoo Mail hoặc Goole Mail trong ASP.net

Tạo chức năng multiDelete giống Yahoo Mail hoặc Goole Mail trong ASP.net

Trong bài viết này, WEBXAULA sẽ giới thiệu tới các bạn cách tạo chức năng xoá cùng lúc nhiều dòng dữ liệu giống Yahoo Mail hoặc Google Mail 

Tạo chức năng multiDelete giống Yahoo Mail hoặc Goole Mail trong ASP.net

Ở đây mình thực hiện trên Gridview. Bạn viết code html trong file aspx như sau:


<div style="padding:5px; text-align:right;">

 <asp:Button ID="cmdXoa" runat="server" Text="Xoá" Height="30px" Width="100px" OnClientClick="javascript:return confirm(Bạn có chắc chắn muốn xoá tin này không?);" onclick="cmdXoa_Click" />

</div>
<asp:GridView ID="DS_Tin" runat="server" AutoGenerateColumns="False"

           BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px"

           CellPadding="4" GridLines="Horizontal" Width="100%">

           <Columns>

               <asp:TemplateField HeaderText="Tiêu đề">

                   <ItemTemplate>

                       <a href=<%# "../admin/?edittin=" + Eval("tinID") %>><%# Eval("tieude") %></a>

                   </ItemTemplate>

               </asp:TemplateField>

               <asp:TemplateField HeaderText="Hình">

                   <ItemTemplate>

                       <img src=<%# "../hinh/tin/" + Eval("hinh") %> width="80px" />

                   </ItemTemplate>

               </asp:TemplateField>

               <asp:TemplateField HeaderText="Xoá">

                   <ItemTemplate>

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

                       <asp:Label ID="lblMa" runat="server" Text=<%# Eval("tinID") %> Visible="false"></asp:Label>

                   </ItemTemplate>

               </asp:TemplateField>

           </Columns>

           <FooterStyle BackColor="White" ForeColor="#333333" />

           <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />

           <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />

           <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />

        </asp:GridView>

Trong cột "Xoá" ở trên, có 2 control Checkbox "chkXoa" dùng để check chọn vào dòng dữ liệu bạn muốn xoá và Label "lblMa" dùng để lấy mã của dòng dữ liệu xoá.

Ở Button "cmdXoa" tôi thêm một sự kiện OnClientClick và chèn vào một câu lệnh javascript trả về một confirm để xác nhận lại từ người dùng có thực sự muốn xoá dữ liệu hay không.

Trong sự kiện click của button bạn code như sau:

protected void cmdXoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= DS_Tin.Rows.Count - 1; i++)

        {
            CheckBox pXoa = (CheckBox)DS_Tin.Rows[i].FindControl("chkXoa");

            Label pMa = (Label)DS_Tin.Rows[i].FindControl("lblMa");

            if (pXoa.Checked == true)

            {
                //Xoá

                ketnoiDataContext pKetNoi = new ketnoiDataContext();

                Tin pTinXoa = pKetNoi.Tins.Single(p => p.tinID == Int32.Parse(pMa.Text));

                pKetNoi.Tins.DeleteOnSubmit(pTinXoa);

                pKetNoi.SubmitChanges();
            }
        }
        hienDuLieu();
    }

Ở đây tôi dùng Linq để truy vấn dữ liệu. Bạn có thể tuỳ biến lại theo cách xử lý dữ liệu của riêng bạn. Chúc các bạn thành công.

 

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 

Những điều cần biết về nofollow và follow

Trong khi SEO và nhất là Off-page SEO, chúng ta vẫn thường được cảnh báo với nofollow, external nofollow hay đại loại, vậy, nofollow là gì và webmaster cần chú ý gì đến nofollow trong khi xây dựng liên kết.

công ty Snapseed

Google đã chính thức mua lại công ty Snapseed

Theo trang tin Cnet, vào ngày hôm qua, gã khổng lồ trong lĩnh vực tìm kiếm trực tuyến Google đã chính thức mua lại công ty đứng đằng sau ứng dụng chỉnh sửa và chia sẻ hình ảnh trên iOS Snapseed - một trong những đối thủ sừng sỏ của Instagram - nhằm mục tiêu cạnh tranh trực tiếp với Facebook.

Giới thiệu PHP và JavaScript Closures, cài đặt Drupal 8

Giới thiệu PHP và JavaScript Closures, cài đặt Drupal 8

They are automatically constructed and once constructed they can not be changed and the only thing you can do

Công ty diệt chuột T&C

 

Diet con trung