1 / 29

CHƯƠNG VII: KẾT GÁN DỮ LIỆU

CHƯƠNG VII: KẾT GÁN DỮ LIỆU. Lý thuyết : 3 tiết Thực hành 12 tiết. Data Controls. DataGrid =Table(động được tạo trên server) Repeater DataList GridView. Các properties của Data Controls. Student ID. HeadText. 03CDTH892. DataField. ……. ……. 03CDTH632. DataField. DataGrid.

Download Presentation

CHƯƠNG VII: KẾT GÁN DỮ LIỆU

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHƯƠNG VII:KẾT GÁN DỮ LIỆU Lý thuyết : 3 tiết Thực hành 12 tiết

  2. Data Controls • DataGrid =Table(động được tạo trên server) • Repeater • DataList • GridView

  3. Các properties của Data Controls

  4. Student ID HeadText 03CDTH892 DataField ……. ……. 03CDTH632 DataField DataGrid • Ý nghĩa: Cung cấp một dạng bảng tính dữ liệu từ data source • Gồm tập các cột, một header, một footer. • Các cột có thể tự động tạo hoặc tường minh • Thuộc tính: <asp:BoundColumn DataField=“IDs” HeadText=“Student ID” />

  5. Ví dụ: • Tạo điều khiển (Design) • Ràng buộc vào DataReader/Dataset: (Coding) <asp:DataGrid id="dgAuthors" runat="server" /> dgAuthors.DataSource = ds.Table[0]; dgAuthors.DataBind(); dgAuthors.DataSource = dr; dgAuthors.DataBind();

  6. DataGrid – Ví dụ <form id="form1" runat="server"> <div> <asp:DataGrid ID="DataGrid1" runat="server" Style="z-index: 100; left: 55px; position: absolute;top: 15px"> </asp:DataGrid> </div> </form>

  7. DataGrid Editing • Sử dụng EditCommandColumn • Thuộc tínhEditItemIndex (-1 ->không có dòng nào được hiệu chỉnh) • EditCommandColumn hiển thị 3 liên kết tương ứng 3 sự kiện: OnEditCommand, OnUpdateCommand, OnCancelCommand

  8. DataGrid Editing • DataGridCommandEventArgs chứa thông tin của dòng sẽ được hiệu chỉnh • Thuộc tính DataKeyField thiết lập khóa chính • Sự kiện UpdateCommand, truy xuất tập các DataKeys để thao tác trên khóa chính

  9. GridView các sự kiện OnEditCommand=“FunctionEdit" OnCancelCommand=“FunctionCancel“ OnPageIndexChanged=“FunctionPage" OnDeleteCommand=“FunctionDelete" OnItemCommand=“FunctionCommand"

  10. Gán dữ liệu Access vào DataGrid protected void Page_Load(object sender, EventArgs e) { String strCon = "Provider=Microsoft.Jet.OleDb.4.0;Data Source = "; strCon = strCon + Server.MapPath(".") + "\\App_Data\\SV.mdb"; OleDbConnection con = new OleDbConnection(strCon); con.Open(); String strOLE = "select * from tblSinhVien"; OleDbDataAdapter adap = new OleDbDataAdapter(strOLE, con); DataSet ds = new DataSet(); adap.Fill(ds); DataGrid1.DataSource = ds; //kết gán dl với GridView có id là GridView1 DataGrid1.DataBind(); } // lưu ý using System.Data.OleDb;

  11. Gán dữ liệu SQL vào DataGrid protected void Page_Load(object sender, EventArgs e) { string strconn = "server=TRUCLY-PC\\SQL2005;database=SINHVIEN;uid=sa;pwd=sa"; SqlConnection conn = new SqlConnection(strconn); string sql = "SELECT * FROM tblsinhvien"; SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); SqlDataAdapter adap=new SqlDataAdapter(sql,conn); DataSet ds = new DataSet(); adap.Fill(ds); DataGrid1.DataSource = ds; //kết gán dl với DataGrid1 DataGrid1.DataBind(); }

  12. Hiệu chỉnh dữ liệu - DataGrid

  13. Hiệu chỉnh DataGrid – Mã lệnh protectedoverridevoid OnLoad(EventArgs e) { if (!IsPostBack) BindGrid(); base.OnLoad(e); } publicvoid gd1_Edit(object src, DataGridCommandEventArgs e) { gd1.EditItemIndex = (int)e.Item.ItemIndex; BindGrid(); } publicvoid gd1_Cancel(object src, DataGridCommandEventArgs e) { gd1.EditItemIndex = -1; BindGrid(); }

  14. Đặc điểm khác của DataGrid • Tùy biến giao diện GridView • ButtonColumn: hiển thị nút nhấn nhằm thực thi một lệnh nào đó. • Thuộc tính AllowSorting: hiển thị tiêu đề cột như một siêu liên kết. • Thuộc tính SortField : chỉ ra field được sắp xếp.

  15. PROPERTY DATAGRID

  16. Định dạng Datagrid

  17. Thực hành: Sử dụng DataGrid • Tạo một OleDbConnection • Tạo một OleDbCommand • Tạo a DataReader • Kéo một DataGrid vào Web Form • Ràng buộc DataSource vào DataGrid

  18. Repeater • Ý nghĩa: • Ràng buộc dữ liệu từ danh sách các item. Hiển thị dữ liệu dựa trên các phần tử HTML • Templates:

  19. DataBinder • Phân tích và đánh giá biểu thức ràng buộc dữ liệu • Tính toán biểu thức ràng buộc dữ liệu để rút trích dữ liệu đúng trong template • Container.DataItem trả về đối tượng Object • DataBinder.Eval() là hàm tĩnh sử dụng Reflection • Không quan tâm đến kiểu của nguồn dữ liệu (data source)

  20. DataBinder – Ví dụ <asp:Repeaterid="rp1"runat="server"> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "Name") %><br> <%# DataBinder.Eval(Container.DataItem, "Age","{0:2d}") %> <br> </ItemTemplate> </asp:Repeater>

  21. A Repeater control in use

  22. DataList • Sử dụng DataList hiển thị dữ liệu từ data source • Sử dụng khi hiển thị dữ liệu dạng danh sách (tương tự dạng Table) • Đơn giản hơn DataGrid • Ít nhất có ItemTemplate • Mỗi template có kiểu dáng riêng (HeaderStyle và ItemStyle)

  23. DataList – Ví dụ

  24. Datalist

  25. Thuộc tính Mô tả DataSource • Nguồn dữ liệu DataTextField • Field trong DataTable được hiển thị DataValueField • Field trong DataTable trở thành giá trị của item Ràng buộc dữ liệu từ Data Reader • Thiết lập thuộc tính • Gọi phương thức DataBind Id.DataSource = dr; dgEmp.DataBind();

  26. Minh họa: Điều khiển DataGrid • Sử dụng AutoFormat • Thiết lập Tiêu đề cột • Phân trang • Sắp xếp

  27. Dataset là gì? DataSet DataTable DataTable DataTable OleDbDataAdapter Bộ nhớ Thiết bị lưu trữ OleDbConnection OleDbDataAdapter OleDbConnection OleDb Server 2000 OleDb Database

  28. DataSet Truy cập dữ liệu với ADO.NET Database 1 Yêu cầu từ Client Tạo đối tượng kết nối OleDbConnection và OleDbDataAdapter 2 Điền dữ liệu vào DataSet từ DataAdapter, đóng kết nối OleDbConnection 3 Web server Trả DataSet cho Client 4 OleDbDataAdapter Client thao tác dữ liệu 5 Cập nhật DataSet 6 Sử dụng OleDbDataAdapter để mở OleDbConnection, cập nhật cơ sở dữ liệu và đóng kết nối 7 List-Bound Control Client

More Related