Các ví dụ mở rộng

Một phần của tài liệu Giáo trình môn họcmô đun lập trình web với ASP NET (Trang 130)

IV.1. Xửlý đảo hƣớng sắp xếp trong DataGrid

Ví dụ minh họa dưới đây xử lý đảo hướng sắp xếp trong DataGrid. Đồng thời, trong ví dụ này, chúng tôi thực hiện liên kết dữ liệu qua đối tượng DataView để thực hiện sắp xếp trên nguồn dữ

liệu.

Private Sub Page_Load(…, e …) Handles MyBase.Load

If Not IsPostBack Then Lien_ket_du_lieu() End Sub

Public Sub Lien_ket_du_lieu()

Dim dtKhach_hang As DataTable = Doc_ds_khach_hang() Dim dvKhach_hang As New

DataView(dtKhach_hang) dvKhach_hang.Sort = ViewState("SortExpression") If

ViewState("SortAscending") = "false" Then dvKhach_hang.Sort &= " desc" End If

dtgKhach_hang.DataSource = dvKhach_hang

dtgKhach_hang.DataBind() End Sub

Public Function Doc_ds_khach_hang() As DataTable Dim sKet_noi As String

sKet_noi = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & _ Server.MapPath("..\Data\QlBanSach.mdb")

Dim cnKet_noi As New OleDbConnection(sKet_noi) Dim dsCSDL As New DataSet

'Mở và đóng kết nối ngay khi thực hiện xong cnKet_noi.Open()

Dim daBo_doc_ghi As New OleDbDataAdapter _ ("Select * From KHACH_HANG", cnKet_noi) cnKet_noi.Close()

daBo_doc_ghi.Fill(dsCSDL, "KHACH_HANG") Return dsCSDL.Tables("KHACH_HANG") End Function

Private Sub dtgKhach_hang_SortCommand(…, e …) …

Dim sSap_xep As String = ViewState("SortExpression") Tài liệu hướng dẫn giảng dạy

Trang 125 Dim sHuong As String = ViewState("SortAscending")

ViewState("SortExpression") = e.SortExpression If (e.SortExpression = sSap_xep) Then

ViewState("SortAscending") = IIf(sHuong = "false", "true", "false") End If

Lien_ket_du_lieu() End Sub

IV.2. Tạo biểu tƣợng sắp xếp trong cột cho DataGrid Sắp xếp tăng dần theo tên khách hàng

Private Sub dtgKhach_hang_ItemDataBound(…, e …) …

If e.Item.ItemType = ListItemType.Header Then

Dim sSap_xep As String = ViewState("SortExpression") Dim sHuong As String = ViewState("SortAscending") Dim sKy_hieu As String = IIf(sHuong = "false", " 6", " 5") Dim i%

For i = 0 To dtgKhach_hang.Columns.Count - 1 If sSap_xep = _

dtgKhach_hang.Columns(i).SortExpression Then Dim cell As TableCell = e.Item.Cells(i)

Dim lblKy_hieu As New Label lblKy_hieu.Text = sKy_hieu lblKy_hieu.Font.Name = "webdings" lblKy_hieu.Font.Size = FontUnit.XSmall cell.Controls.Add(lblKy_hieu) End If Next End If End Sub

IV.3. Định dạng hình thức hiển thị cho dòng dữ liệu thỏa điều kiện trên DataGrid (adsbygoogle = window.adsbygoogle || []).push({});

Trong ví dụ sau, chúng ta thực hiện tô màu cho những khách hàng có tên bắt đầu bằng ký tự H.

Private Sub dtgKhach_hang_ItemDataBound(…, e …) …

If e.Item.ItemIndex < 0 Then Exit Sub Dim sTen_kh As String

sTen_kh = e.Item.DataItem("Ten_khach_hang") 'Tiến hành kiểm tra điều kiện,

Trang 126 If sTen_kh.StartsWith("H") Then e.Item.BackColor = Color.LemonChiffon e.Item.Cells(1).Font.Bold = True End If End Sub

Tô màu những khách hàng có tên bắt đầu bằng ký tự H

IV.4. Tạo hiệu ứng chọn khi rê chuột qua các dòng dữ liệu Private

Sub dtgKhach_hang_ItemDataBound(…, e …) …

If e.Item.ItemIndex < 0 Then Exit Sub e.Item.Attributes("onMouseOver") = _ "this.style.backgroundColor='#FFF8DC'" e.Item.Attributes("onMouseOut") = _ "this.style.backgroundColor=''" End Sub Tạo hiệu ứng chọn dòng dữ liệu trên lƣới Kinh nghiệm giảng dạy:

Các điều khiển liên kết dữ liệu (DataGrid, DataList, Repeater) hỗ trợ khá tốt việc hiển thị

dữ liệu trên trang web.

Do hỗ trợ khá nhiều chức năng, giáo viên nên hướng dẫn học viên sử dụng tuần tự từng chức năng màcác điều khiển hỗ trợ. Sau khi nắm vững các thao tác của từng chức năng,

Trang 127

TÀI LIỆU THAM KHẢO

1. MSDN Library - April 2003 & MSDN Library - July 2005

2. MSDN Traning: Developing Microsoft ASP.NET Web Applications Using Visual Studio.NET

3. MSDN Traning: Programming with Microsoft ADO.NET 4. ASP.NET Web Developer’s Guide

5. ASP.NET By Example [Steven A. Smith]

6. Developing Web Applications with Visual Basic .NET and ASP.NET [John Alexander, Billy Hollis]

7. Programming ASP.NET, 2nd Edition [Dan Hurwitz, Jesse Liberty] 8. Inside ASP.NET [Scott Worley]

9. ASP NET Bible [Mridula Parihar]

10. ASP.NET for Web Designers [Peter Ladka] 11. Professional ADO.NET Programming [Wrox]

12. Cascading Style Sheets - The Designer's Edge [Molly E. Holzschlag ] 13. JavaScript Bible - Gold Edition [Danny Goodman]

14. Real World Web Services [Yasser Shohoud] 15. Trang chủ ASP.Net: http://www.asp.net

Một phần của tài liệu Giáo trình môn họcmô đun lập trình web với ASP NET (Trang 130)