Cập nhật dữ liệu trực tiếp ngay trên gridview trong asp.net website

Cập nhật dữ liệu trực tiếp ngay trên gridview trong asp.net website

Lần trước WEBXAULA đã giới thiệu với các bạn cách chọn xoá nhiều dòng dữ liệu trên gridview. Trong bài viết này, tôi sẽ giới thiệu với các bạn cách cập nhật dữ liệu trực tiếp ngay trên gridview 

Cập nhật dữ liệu trực tiếp ngay trên gridview trong asp.net website

 Trong ví dụ này, tôi sẽ cập nhật họ tên, địa chỉ, số điện thoại và trạng thái thành viên của các thành viên đã đăng ký.

<asp:GridView ID="DS_ThanhVien" runat="server" AutoGenerateColumns="False"

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

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

            onrowdatabound="DS_ThanhVien_RowDataBound"

            onrowdeleting="DS_ThanhVien_RowDeleting"

            onrowupdating="DS_ThanhVien_RowUpdating">

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

            <Columns>

                <asp:TemplateField HeaderText="Email">

                    <ItemTemplate>

                        <%# Eval("email") %>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Họ tên">

                    <ItemTemplate>

                        <asp:TextBox ID="txtHoTen" Text=<%# Eval("hoten") %> Width="90%" ToolTip=<%# Eval("hoten") %>runat="server"></asp:TextBox>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Địa chỉ">

                    <ItemTemplate>

                        <asp:TextBox ID="txtDiaChi" Text=<%# Eval("diachi") %> Width="90%" ToolTip=<%# Eval("diachi") %>runat="server"></asp:TextBox>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Điện thoại">

                    <ItemTemplate>

                        <asp:TextBox ID="txtDienThoai" Text=<%# Eval("dienthoai") %> Width="90%" ToolTip=<%# Eval("dienthoai") %>runat="server"></asp:TextBox>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField>

                    <ItemTemplate>

                        <asp:ImageButton ID="cmdSave" CommandName="Update" ImageUrl="../hinh/tour_save_button.png" runat="server"/>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Tình trạng">

                <ItemStyle HorizontalAlign="Center" />

                    <ItemTemplate>

                        <asp:ImageButton ID="cmdTrangThai" CommandName="Delete" ImageUrl="../hinh/check_16.png" runat="server" />

                        <asp:Literal ID="lblTrangThai" runat="server" Text=<%# Eval("IsUse") %> Visible="false"></asp:Literal>

<asp:Label ID="lblMa" runat="server" Visible="false" Text=<%# Eval("ThanhVienID") %>></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>

Ở đây tôi dùng textbox hiển thị dữ liệu để người dùng có thể chỉnh sửa trực tiếp. Trên mỗi dòng của gridview có một button với command Name và sau đó ta tạo sự kiện tương ứng cho button đó. Một label hiển thị mã thành viên để truy vấn dữ liệu trên mã đó.

Trong sự kiện RowDataBound của Gridview:

protected void DS_ThanhVien_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex < 0) return;
        ImageButton cmdTrangThai = (ImageButton) e.Row.FindControl("cmdTrangThai");
 
        if (DataBinder.Eval(e.Row.DataItem, "IsUse").ToString() == "1")
        {
            cmdTrangThai.ImageUrl = "../hinh/check_16.png";
            cmdTrangThai.ToolTip = "Đã kích hoạt tài khoản";
            //cmdTrangThai.Enabled = false;
        }
        else
        {
            cmdTrangThai.ImageUrl = "../hinh/delete.jpg";
            cmdTrangThai.ToolTip = "Chưa kích hoạt tài khoản";
            //cmdTrangThai.Enabled = true;
        }
 
    }
 

Sự kiện RowUpdating của gridview phục vụ cho việc cập nhật dữ liệu

 
protected void DS_ThanhVien_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txtHoTen = (TextBox)DS_ThanhVien.Rows[e.RowIndex].FindControl("txtHoTen");
        TextBox txtDiaChi = (TextBox)DS_ThanhVien.Rows[e.RowIndex].FindControl("txtDiaChi");
        TextBox txtDienThoai = (TextBox)DS_ThanhVien.Rows[e.RowIndex].FindControl("txtDienThoai");
        Label lblMa = (Label)DS_ThanhVien.Rows[e.RowIndex].FindControl("lblMa");
 
        ketnoiDataContext pKetNoi = new ketnoiDataContext();
        ThanhVien pThanhVienCapNhat = pKetNoi.ThanhViens.Single(p => p.ThanhVienID == Int32.Parse(lblMa.Text));
        pThanhVienCapNhat.HoTen = txtHoTen.Text;
        pThanhVienCapNhat.DienThoai = txtDienThoai.Text;
        pThanhVienCapNhat.DiaChi = txtDiaChi.Text;
        pKetNoi.SubmitChanges();
 
        lblThongBao.Text = "Bạn đã cập nhật thành công";
 
    }
 

Ở đây tôi dùng linq để truy vấn dữ liệu, bạn có thể sửa lại cho phù hợp với code của bạn.

Sự kiện RowDeleting: Thay đổi trạng thái của thành viên

protected void DS_ThanhVien_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ImageButton cmdTrangThai = (ImageButton)DS_ThanhVien.Rows[e.RowIndex].FindControl("cmdTrangThai");
        Literal lblTrangThai = (Literal)DS_ThanhVien.Rows[e.RowIndex].FindControl("lblTrangThai");
        Label lblMa = (Label)DS_ThanhVien.Rows[e.RowIndex].FindControl("lblMa");
 
       
            ketnoiDataContext pKetNoi = new ketnoiDataContext();
            ThanhVien pThanhVienCapNhat = pKetNoi.ThanhViens.Single(p => p.ThanhVienID == Int32.Parse(lblMa.Text));
            if (lblTrangThai.Text == "0")
                pThanhVienCapNhat.IsUse = 1;
            else
                pThanhVienCapNhat.IsUse = 0;
 
            pKetNoi.SubmitChanges();
 
            HienDuLieu();
 
 
 
    }

 

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

 
 Hướng dẫn đổi IP trên hệ thống Debian

Hướng dẫn đổi IP trên hệ thống Debian

Em chỉ mới gia nhập hệ thống NAS dc vài ngày nên vẫn trong quá tình tìm hiểu và học hỏi nhưng trong quá trình tìm hiểu em thấy nhiều bạn không biết cách đổi IP tĩnh trên hệ thống Debian

5 bài học Web Design từ hệ thống E-commerce Store

5 bài học Web Design từ hệ thống E-commerce Store

As a content writer, I never thought I'd be running an e-commerce store, but when a brilliant product – and the perfect branding concept - fell into my lap earlier this year, I had to take the plunge.

Allow Drupal Users to Manage Their Own Content

Cho phép Drupal Users tự quản lý nội dung của mình

This week's tutorial is the first of a two-parter. We've had several students in our classes looking to build websites with multiple content authors ... blogs, newspapers, university sites and more. A common request is to improve Drupal's default handling of nodes.

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

 

Diet con trung