Tải bản đầy đủ (.pdf) (10 trang)

Sừ dụng Detailview và formview

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (286.44 KB, 10 trang )


195
ID="SqlDataSource1" runat="server"></asp:SqlDataSource>

</div>

</form>

</body>

</html>

Kết xuất của chương trình

Sử dụng Fields với điều khiển GridView

• BoundField: cho phép bạn hiển thị giá trị của các mục dữ liệu dạng Text

• CheckBoxField: cho phép bạn hiển thị giá trị của dữ liệu dưới dạng CheckBox.

• CammandField: hiển thị 1 liên kết cho phép chỉnh sửa, xoá hay chọn dòng dữ liệu

• ButtonField: Cho phép hiển thị dữ liệu như một Button(Button, ImageButton,
linkButton, Push Button)

• HyperLinkButton: Cho phép hiển thị dữ liệu như một liên kết đến một trang web khác.

• ImagesField: Cho phép bạn hiển thị dữ liệu như một Ảnh

• TemplateField: cho phép bạn hiển thị dữ liệu một cách tuỳ biến với các thẻ HTML hoặc
ASP.NET



Chương 11 Sử dụng DetailView và FormView

Hai điều khiển này cho phép bạn làm việc với một trường dữ liệu đơn tại mỗi thời điểm

Cả hai điều khiển này cho phép bản thay đổi, thêm mới hay xoá dữ liệu như một bản
ghi cơ sở dữ liệu, và nó cho phép bạn chuyển sang trang tiếp theo hay quay lại trang trước
thông qua thiết lập dữ liệu.

I. DetailView

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

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
" />
<html xmlns=" >


196
<head runat="server">


<title>Detail View</title>

</head>

<body>

<form id="form1" runat="server">

<div id="navcontain">

<asp:DetailsView ID="DetailsView1"

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

</asp:DetailsView>

<asp:SqlDataSource ID="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ó

Kết xuất của chương trình sẽ như sau:


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


197
Ví dụ:

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

Code 11.2

using System;

public class Employee

{

private int _PersonID;

public int PersonID

{


get { return _PersonID; }

set { _PersonID = value; }

}

private string _Hoten;

public string Hoten

{

get { return _Hoten; }

set { _Hoten = value; }

}

private int _Tuoi;

public int Tuoi

{

get { return _Tuoi; }

set { _Tuoi = value; }

}


public Employee()

{

}


198
public Employee(int _PersonID, string _Hoten, int _Tuoi)

{

this._PersonID = _PersonID;

this._Hoten = _Hoten;

this._Tuoi = _Tuoi;

}

}

Code 11.3 DetailViewPerson.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="DetailViewPerson.aspx.cs" Inherits="DetailViewPerson" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
" />
<html xmlns=" >


<head runat="server">

<title>Detail View</title>

</head>

<body>

<form id="form1" runat="server">

<div id="navcontain">

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

</asp:DetailsView>

</div>

</form>

</body>

</html>

Code 11.4 DetailViewPerson.aspx.cs

using System;


using System.Collections;


199
using System.Collections.Generic;

using System.Data;

public partial class DetailViewPerson : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

Employee newEmploy=new Employee(1,"HCUBIU",25);

List<Employee> listEmploy=new List<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.

2. Sử dụng Fields với điều khiển DetailView

DetailView hỗ trợ tất cả các Field như GridView



BoundField: cho phép bạn hiển thị giá trị của dữ liệu như Text



CheckBoxField: hiển thị dữ liệu dưới dạng một CheckBox



CommandField: hiển thị liên kết cho phép chỉnh sửa, thêm mới, xoá dữ liệu của
dòng.



ButtonField: hiển thị dữ liệu như một button(ImageButton, )




HyperLinkField: hiển thị môt liên kết



ImageField: hiển thị ảnh

×