Hiển thị dữ liệu với DetailView

Một phần của tài liệu GIÁO TRÌNH ASP. NET potx (Trang 121 - 123)

V. Sử dụng điều khiển BulletedList

1.Hiển thị dữ liệu với DetailView

DetailView được đưa ra hiển thị như một bảng(<Table>) trong HTML để hiển thị dữ liệu một bản ghi.

Ví dụ: Trang DetailView.aspx Code 11.1

<%@PageLanguage="C#"AutoEventWireup="true" CodeFile="DetailView.aspx.cs" Inherits="_DetailView" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml"> <headrunat="server">

<title>Detail View</title> </head>

<body>

<formid="form1"runat="server">

<divid="navcontain">

<asp:DetailsViewID="DetailsView1"

DataSourceID="SqlDataSource1"runat="server"Height="50px"Width="125px">

</asp:DetailsView>

<asp:SqlDataSourceID="SqlDataSource1"

ConnectionString="<%$ConnectionStrings:hcubiuData %>"

SelectCommand="select * from tblIntrodure"runat="server"></asp:SqlDataSource>

</div>

</form> </body> </html>

Vẫn với cơ sở dữ liệu từ chương trước bạn đưa dữ liệu của bảng tblIntrodure vào SqlDataSource và điền nó vào DetailView1 với thuộc tính DataSourceID của nó

Bạn cũng có thể đưa dữ liệu vào DetailView từ một mảng hay danh sách dữ liệu Ví dụ:

Bạn tạo một lớp Employee.cs Code 11.2

using System;

publicclassEmployee {

privateint _PersonID; publicint PersonID {

get { return _PersonID; } set { _PersonID = value; } }

privatestring _Hoten; publicstring Hoten {

get { return _Hoten; } set { _Hoten = value; } }

privateint _Tuoi; publicint Tuoi {

get { return _Tuoi; } set { _Tuoi = value; } }

public Employee() {

}

public Employee(int _PersonID, string _Hoten, int _Tuoi) { (adsbygoogle = window.adsbygoogle || []).push({});

this._PersonID = _PersonID; this._Hoten = _Hoten; this._Tuoi = _Tuoi;

} }

Code 11.3 DetailViewPerson.aspx

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="DetailViewPerson.aspx.cs" Inherits="DetailViewPerson" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml"> <headrunat="server">

<title>Detail View</title> </head>

<body>

<formid="form1"runat="server">

<divid="navcontain">

<asp:DetailsViewID="DetailsView1"runat="server"Height="50px"Width="125px">

</asp:DetailsView> </div> </form> </body> </html> Code 11.4 DetailViewPerson.aspx.cs using System; using System.Collections; using System.Collections.Generic; using System.Data;

publicpartialclassDetailViewPerson : System.Web.UI.Page {

protectedvoid Page_Load(object sender, EventArgs e) {

if (!IsPostBack) {

Employee newEmploy=newEmployee(1,"HCUBIU",25); List<Employee> listEmploy=newList<Employee>(); listEmploy.Add(newEmploy); DetailsView1.DataSource = listEmploy; DetailsView1.DataBind(); } } }

Trong ví dụ này chúng ta tạo ra một lớp Employee và chúng ta đưa dữ liệu vào DetailView1 với thuộc tính DataSource và phương thức DataBind điền dữ liệu vào.

Một phần của tài liệu GIÁO TRÌNH ASP. NET potx (Trang 121 - 123)