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

 
3 cách sắp xếp lại các fields trong Drupal

3 cách sắp xếp lại các fields trong Drupal

With all the software we teach, some tasks almost always confuse beginners.

30 iPhone khổng lồ vinh danh 'nhà phát minh' Steve Jobs

30 iPhone khổng lồ vinh danh 'nhà phát minh' Steve Jobs

Cơ quan Bản quyền và Thương hiệu Mỹ (U.S. Patent and Trademark Office - USPTO) thực hiện một triển lãm độc đáo dành cho cựu CEO Apple

Number 5: Maintenance - 5 lỗi cần tránh trong Drupal

Number 5: Maintenance - 5 lỗi cần tránh trong Drupal

In the previous articles in this series, we've focused on aspects of architecture, security and performance and the choice of infrastructure. All of that work will come to little without a reliable maintenance plan

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

 

Diet con trung