Hướng dẫn sử dụng lồng các control trong ASP.Net

Hướng dẫn sử dụng lồng các control trong ASP.Net

Khi xây dựng một website, nhiều khi bạn phải thực hiện lồng các control để hiển thị dữ liệu, vd như hiển thị các bài viết theo đúng danh mục của nó. Ở đây, WEBXAULA sẽ minh hoạ cách lồng một Datalist trong một Gridview.

Hướng dẫn sử dụng lồng các control trong ASP.Net

 Đầu tiên, bạn chuẩn bị cơ sở dữ liệu mẫu như sau:

Database - Sử dụng lồng các control

Code html trong trang aspx viết như sau:

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

            ShowHeader="false" onrowdatabound="DS_DanhMuc_RowDataBound">

            <Columns>

                <asp:TemplateField>

                <ItemTemplate>

                    <%# Eval("danhmucName") %>

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

<asp:DataList ID="DS_SanPham" runat="server" RepeatColumns="3">

                    <ItemTemplate>

                        <img width="100px" src=<%# "hinh/sanpham/" + Eval("hinh") %> />

                        <br />

                        <%# Eval("sanphamName") %>

                    </ItemTemplate>

                    </asp:DataList>


                </ItemTemplate>

                </asp:TemplateField>

            </Columns>

        </asp:GridView>
 
Code C#: 

Bạn viết hàm để lấy dữ liệu hiển thị cho phần danh mục, ở đây tôi dùng Linq để tiến hành truy vấn dữ liệu.

private void HienDuLieu()
    {
        try

        {

            ketnoiDataContext pKetNoi = new ketnoiDataContext();

            var pDanhMuc = from p in pKetNoi.DanhMucs

                           orderby p.DanhMucID descending

                           select p;

            DS_DanhMuc.DataSource = pDanhMuc;

            DS_DanhMuc.DataBind();

        }

        catch (Exception ex)

        {

            Response.Write(ex.Message.ToString());

        }

    }

Gridview bao bên ngoài ta sẽ tạo sự kiện RowDataBound để xử lý từng dòng dữ liệu được bind vào gridview.

protected void DS_DanhMuc_RowDataBound(object sender, GridViewRowEventArgs e)

    {
        if (e.Row.RowIndex < 0) return;

        DataList DS_SanPham = (DataList)e.Row.FindControl("DS_SanPham");

        Literal lblMa = (Literal)e.Row.FindControl("lblMa");

        ketnoiDataContext pKetNoi = new ketnoiDataContext();

        var pSanPham = from p in pKetNoi.SanPhams

                       where p.DanhMucID == Int32.Parse(lblMa.Text)

                       select p;

        DS_SanPham.DataSource = pSanPham;

        DS_SanPham.DataBind();

    }

Bạn chú ý trong Gridview tôi có để một literal để lấy mã của danh mục tương ứng và từ đó hiển thị sản phẩm lên Datalist theo đúng danh mục đó. Do không cần hiển thị nên literal này ta set visible = false.

Về giao diện thì bạn có thể tuỳ chỉnh sao cho đẹp mắt là được. Chúc các bạn thành công.

Bạn thấy bài viết này như thế nào?: 
Average: 10 (2 votes)
Ả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

 
Phễu traffic - Phễu bán hàng trở thành một xu thế tất yếu trong Marketing Online

Phễu traffic - Phễu bán hàng trở thành một xu thế tất yếu trong Marketing Online

Phễu bán hàng đang trở thành một xu thế tất yếu trong Marketing Online, bạn cần hiểu vào áp dụng ngay vào doanh nghiệp của bạn

Lucidly Drupal: Setting up Ubuntu 10.4 Lucid LAMP stack for your Drupal site

Lucidly Drupal: Setting up Ubuntu 10.4 Lucid LAMP stack for your Drupal site

We're frequently setting up servers for development, for staging, for production. I've lately preferred the Debian flavor of Linux, but up until now that had been something of a problem because Debian and Ubuntu did not include the higher-quality php5-gd library, which meant that you either had to compile your own PHP, pull from an alternative source host, or cope with substandard image resizing with limited processing features.

Hướng dẫn làm Responsive cho Commerce Checkout Tables trong Drupal 7

Hướng dẫn làm Responsive cho Commerce Checkout Tables trong Drupal 7

Providing a responsive checkout is an important feature of any eCommerce site. Drupal Commerce if themed correctly handles this fairly sweet; except for one element, tables.

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

 

Diet con trung