Bài giảng HTML Server Control và Web Server Control
Các HTML control thông thường như<h1>,
<a>, <input> sẽkhôngđượcxửlý bởiserver
màđượcgửitrựctiếpchobrowserđểhiểnthị
CácHTML controlcó thểđượcxửlý ngay tại
phía server bằng cách chuyển chúng thành các
HTML server control
ntửđựơcchọn
Sự kiện: SelectedIndexChaged
Với i=0, n-1 (n=Items.Count).
7
5/10/2013
II. Web Server Control II. Web Server Control
DataSource: thiết lập giá trị từ DataSource như
DataTable,DataSet,Array,Collection,DataView
Các đặc tính Checkbox RadioButton DropDown
List List List ListBox
DataTextField: thiết lập phần tử text từ DataSource
Chọn 1 Item duy nhất XX
DataValueField: thiết lập giá trị phần tử từ Chọn hơn 1 Item XX
DataSource Hiển thị toàn bộ danh
sách X
DataBind(): binding data vào ListControl
(Là các thông tin
Ch
ương 7 ) Data
l
ấy đượ
c t
ừ CSDL t
ại
II. Web Server Control II. Web Server Control
Ví dụ: DropDownList protected void btnThem_Click(object sender,
EventArgs e)
{
drpLan.Items.Add(txtThem.Text);
Anh }
Pháp protected void Page_Load(object sender, EventArgs e)
Hoa {
if(drpLan.SelectedItem!=null)
lblKq.Text="The selected item is: " +
drpLan.SelectedItem.Text;
else
lblKq.Text="No item is selected";
}
8
5/10/2013
II. Web Server Control II. Web Server Control
Ví dụ: CheckBoxList protected void Page_Load(object sender, EventArgs e)
<asp:CheckBoxList ID="chkMonhoc" runat="server" {
string[] maMH ={ "001","002","003","004"};
Width="173px" AutoPostBack="True"> string[] tenMH ={"LT Windows", "LT
Cau hinh Phan Internet","AVCN","Cau hinh Mang"};
cung for (int i = 0; i < maMH.Length; i++)
Co so du {
lieu chkMonhoc.Items.Add(new ListItem(tenMH[i],maMH[i]));
}
}
II. Web Server Control II. Web Server Control
Ví dụ: RadioButtonList protected void Page_Load(object sender, EventArgs e)
<asp:RadioButtonList ID="radFont" runat="server" {
Width="126px" AutoPostBack="True"
OnSelectedIndexChanged="radFont_SelectedIndexChanged"> if (!IsPostBack)
10 {
12 int[] sizefont ={ 18, 20, 22, 24 };
14 for (int i = 0; i < sizefont.Length; i++)
16 radFont.Items.Add(new ListItem(sizefont[i].ToString()));
}
<asp:Label ID="lblKq" runat="server" Height="32px" Text="To }
be or not to be" Width="368px">
9
5/10/2013
II. Web Server Control II. Web Server Control
protected void radFont_SelectedIndexChanged(object sender, Table: Hiểnthị thông tin dướidạng dòng và
EventArgs e)
{ cột.
int sizeselected = Convert.ToInt32(radFont.SelectedValue);
lblKq.Font.Size = sizeselected; Table control cho phép xây dựng các bảng
} động bằng mã lệnh sử dụng các thuộctínhtập
hợp Table Rows và Table Cells
II. Web Server Control II. Web Server Control
Cú pháp AdRotator Server Control :quảng cáo trên
trang web
<asp:Table id="Table1" runat="server"
Thuộc tính:
Width="100px" Height="70px">
ImageUrl: URL củahìnhảnh cần đượchiển
ASP ControlHTML HTMLControl thị
NavigateUrl: URL củatrangwebphải
Table chuyển đến control khi có sự kiện click.
TableRow AlternateText:Dòngvănbảnhiểnthị khi
hình không có sẳn
TableCell
Keyword: loạiquảng cáo
TableHeaderCell
10
5/10/2013
II. Web Server Control II. Web Server Control
FileUpload Server Control: dùng thực hiện upload file lên Ví dụ:
server protected void btnShow_Click(object sender, EventArgs e)
{
Các thuộc tính: lblFliename.Text =
FileName: Tên file được upload lên FileUpload1.PostedFile.FileName.ToString();
lblType.Text =
FileBytes: Mảng bytes chứa nội dung file upload
FileUpload1.PostedFile.ContentType.ToString();
PostedFile: Hiển thị đầy đủ như 1 đối tượng HttpPostedFile. lblLength.Text =
HttpPostedFile có các thuộc tính FileUpload1.PostedFile.ContentLength.ToString();
//Luu file
FileName: Ten File
string filename = FileUpload1.FileName.ToString();
ContentType: Loại File (.doc, .mdb, …) FileUpload1.PostedFile.SaveAs(Server.MapPath("") +
"\\BT_Chuong3_ListControl" + filename);
ContentLength: Kích thước của File.
}
SaveAs: Lưu file upload vào 1 thư mục bất kỳ
II. Web Server Control Validation Server Control
11
5/10/2013
V. Validation Server Control V. Validation Server Control
1. RequiredFieldValidator Server Control: Thu
ộc tính
yêu cầu người dùng bắt buộc phải nhập liệu ControlToValidate: nhận một ID của một
Cú pháp: control khác ở trên form để kiểm tra nhập liệu.
Display: có ba giá trị : none, static, dynamic.
thể hiện thông báo động hay tỉnh.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
EnableClientScript: nhận true thì hiểu các
runat="server“ ControlToValidate="input_Control“ script ở phía client, false thì không.
ErrorMessage=“Content_Message”
ErrorMessage: dòng thông báo khi dữ liệu
không hợp lệ.
Initialvalue: giá trị khởi tạo
V. Validation Server Control V. Validation Server Control
Các s Ví dụ: thiết kế form có dạng như sau, khi click
ự ki nút submit, nếu chưa nhập tên thí xuất hiện
DataBinding()ện
thông báo lỗi
Disposed()
Int()
Load(): Lập trình sự kiện này là để đặt giá trị
ban đầu cho control.
PreRender()
Unload()
12
5/10/2013
Mã chương trình
V. Validation Server Control
2. Range Validator Server Control: Kiểmtra
Enter Your Name:
giớihạnnhậpliệu, giá trị nhậpphảinằmtrong
<asp:TextBox id="txtName" rows="1 " width="50"
runat="server"/> khoảng giớihạnchotrước, giớihạnnàycóthể
là các hằng được đưavàolúcthiếtkế hoặcso
<asp:RequiredFieldValidator id="validTxtName"
runat="server" controlToValidate="txtName“ sánh với các control khác trên trang web
errorMessage="Name must be entered"
display="static">
<asp:Button id="btnSubmit" runat="server"
text="Submit" />
V. Validation Server Control V. Validation Server Control
Cú pháp: Thuộc tính:
<asp:RangeValidator id="RangeValidator1" runat="server“ minimumValue, maximumValue : khoảng
display="static“ controlToValidate=" input_Control " giớihạngiátrị nhậpliệu
errorMessage= " Content_Message " type = “ Integer"
ControlToValidate: chứaIDcủamột
minimumValue=minValue maximumValue=maxValue>
control khác ở trên form để kiểmtragiớihạn
nhậpliệu.
Display: có giá trị none, static, dynamic.
Chọnkiểuthể hiện thông báo.
13
5/10/2013
V. Validation Server Control V. Validation Server Control
3. RegularExpressionValidator Server Control: Thuộc tính:
kiểmtradữ liêu nhậpvới khuôn biểuthứcmẫu ValidationExpression: Khung của biểu thức
(RegularExpression) đã được định nghĩatrước. mẫu để so sánh kiểm tra
Visual Studio .NET cung cấpcáckhuônbiểuthức Cú pháp:
mẫu:
<asp:RegularExpressionValidator …
Telephone numbers
ControlToValidate=“Input_control"…
Postal codes ValidationExpression =“RegularExpression "…>*
E-mail addresses
V. Validation Server Control V. Validation Server Control
4. CompareValidator Server Control: So sánh dữ Thuộc tính
liệunhậpvớimộttrị trong một control khác hoặc
ControlToCompare: chứa ID của một control
mộthằng đượcchotrướckhithiêtkế hoặcmộtgiá mà giá trị của control ID này sẽ so sánh với dữ
trị trong dữ liệu. Các phép toán so sánh liệu của một control khác
>,>=,<,<= ControlToValidate: chứa ID của một control
Cú pháp: mà dữ liệu của control này được so sánh vớI dữ
<asp:CompareValidator id="comID" runat="server" liệu của control ở thuộc tính trên.
display="static“ controlToValidate="txtR" Operator: Toán tử so sánh
errorMessage=" Content_Message " Type : Kiểu dữ liệu để so sánh gồm (String,
type="Double" operator="DataTypeCheck"> Integer, Date, Double, Currency)
ValueToCompare: chứa giá trị so sánh hằng
14
5/10/2013
V. Validation Server Control V. Validation Server Control
Các sự kiện 5. CustomValidator Server Control
DataBinding() Kiểmtratínhhợplệ dữ liệucủamộtcontrol
Disposed() theo một yêu cầu, mộtràngbuộcnàođó, hay
mộtkiểudữ liệu đượcngườisử dụng định
Int()
nghĩatrước đó.
Load(): Lập trình sự kiện này là để đặt giá trị
ban đầu cho control. CustomValidator Server Control có thể kiểmtra
hợplệ cả phía client và server
PreRender()
Unload()
V. Validation Server Control V. Validation Server Control
Các sự kiện
Thuộc tính
DataBinding()
ClientValidationFunction:thuộctínhnày
nó chứamộttênhàm,màhàmnàyđượclập Disposed()
trình ở client (bằng javascript). Int()
ControlToValidate:NhậnIDcủamột Load(): Lập trình sự kiện này là để đặt giá trị
control trên form để kiểmtradữ liệu. ban đầu cho control.
PreRender()
ServerValidate(): sự kiện này được lập trình
trên server để kiểm tra tính hợp lệ của dữ liệu.
Unload()
15
5/10/2013
//Mã giao diện
V. Validation Server Control User ID:
Ví dụ: thiết kế form như sau, yêu cầu kiểm tra số Pin PIN:
<asp:Button id=Button1 text="Save"
OnClick="SaveBtn_Click" runat=server />
<asp:CustomValidator id="CustomValidator1"
ControlToValidate="txtPIN"
OnServerValidate="ServerVerify" runat="server">
Invalid PIN number!
//hàm ServerVerify kiểm tra trên server
void ServerVerify(Object Sender, V. Validation Server Control
ServerValidateEventArgs Value) 6. ValidationSummary Server Control:tập
{ hợp các thông báo lỗitừ tấtcả các điềukhiển
trên mộttrang
if (txtPIN.Text == "A999")
Cú pháp:
Value.IsValid = true;
else <asp:ValidationSummary id="valSummary"
Value.IsValid = false; runat="server" HeaderText="These errors were found"
ShowSummary="True" DisplayMode="List"/>
}
16
5/10/2013
V. Validation Server Control V. Validation Server Control
Thuộc tính Các sự kiện
DisplayMode: cung cấp3định dạng hiểnthị DataBinding()
Messagebox: List ,BulletList, SingleParagraph Disposed()
HeaderText:Dòngtiêuđề cho thông báo
Int()
của các control.
Load(): Lập trình sự kiện này là để đặt giá
ShowMessageBox :Truethìhiện thông báo trị ban đầu cho control.
động, False thì hiện thông báo tĩnh.
PreRender()
ShowSummary: True là hiệnthị control này
khi chạy ứng dụng , False thì không (thường Unload()
dùng nhất.)
17File đính kèm:
Chương 4 - HTML Server Control và Web Server Control.pdf

