Xem mẫu
- 5/10/2013
I. HTML Server Control
CHƯƠNG IV: Các HTML control thông thường như ,
HTML SERVERCONTROL VÀ , sẽ không được xử lý bởi server
WEB SERVER CONTROL mà được gửi trực tiếp cho browser để hiển thị
Các HTML control có thể được xử lý ngay tại
phía server bằng cách chuyển chúng thành các
HTML server control.
I. HTML Server Control I. HTML Server Control
Chuyển một HTML control thành một HTML server Tất cả HTML Server Control phải được đặt
control bằng cách thêm thuộc tính runat=”server” trong tag với thuộc tính
vào trong các tag HTML runat = “server”
Cú pháp:
HTML Server Control tồn tại bên trong không
gian tên System.Web.UI.HtmlControls
Ví dụ:
1
- 5/10/2013
Hệ thống thứ bậc của HTML Server Controls
System.Object I. HTML Server Control
System.Web.UI.Control
HtmlControl Các sự kiện (event) của HTML server Control:
HtmlImage HtmlContainerControl onServerClick
HtmlInputControl HtmlForm onServerChange
,
onStartSelect,…
HtmlInputFile HtmlGenericControl , ...
HtmlSelect
HtmlInputHidden
HtmlTable
Cú pháp:
HtmlInputImage
HtmlTableCell
HtmlInputRadioButton ,
HtmlInputText
HtmlTableRow
HtmlInputButton
HtmlTextArea
Lưu ý: Function không có tham số.
HtmlButton
I. HTML Server Control I. HTML Server Control
Ví dụ: Cách lấy dữ liệu từ các HTML server
controlfield_id.Value
Đối với tag DIV, SPAN dùng thuộc tính
.innerHTML
2
- 5/10/2013
I. HTML Server Control Ví dụ
Ví dụ:
void Button_ServerClick(object sender, EventArgs e)
{
public void btnSend_ServerClick(object MySpan.InnerHtml = “Chao ban : " + myText.Value + ".";
sender,System.EventArgs e) }
{
String strName;
strName=txtName.Value;
}
Hệ thống thứ bậc của Web Server Control
II. Web Server Control System.Web.UI.Control
System.Object
Repeater Xml
Web server control là những tag đặc biệt của
WebControl
ASP.NET. Các control này được xử lý trên server AdRotator LinkButton Image
và đòi hỏi phải có thuộc tính runat= “server” BaseDataList ListControl ImageButton
Web server control tồn tại bên trong không DataGrid RadioButtonList Label
gian tên System.Web.UI.WebControls DataList
CheckBoxList BaseValidator
DropDownList BaseCompareValidator
Button
Cú pháp: Calendar
ListBox
CompareValidator
Panel
CheckBox
Table
RangeValidator
RadioButton CustomValidator
TableCell
HyperLink RegularExpressionValidator
TableHeaderCell
TextBox RequiredFieldValidator
TableRow
ValidationSummary
3
- 5/10/2013
II. Web Server Control
II. Web Server Control
Nhóm control cơ bản Label
Cú pháp chung:
Textbox
Button
CheckBox and Radio
Image, Hyperlink, Panel
List Controls group
Các control cơ bản gồm: Table
AddRotator
FileUpload
Validation
II. Web Server Control II. Web Server Control
Label server control: dùng hiển thị văn bản TextBox:dùng để nhập liệu từ người sử dụng
trên trình duyệt. và hiển thị văn bản chỉ đọc
Thuộc tính:
Text: sử dụng để nhận hoặc gán text Thuộc tính :
ví dụ: AutoPostBack: có 2 giá trị True và False khi
- 5/10/2013
II. Web Server Control II. Web Server Control
TextMode: Loại textbox: singleLine, Sự kiện:
MultiLine, Password Text_Changed()
Text: trả về giá trị hoặc gán giá trị Focus(): cho phép đưa trỏ về phần tử
được chỉ định trên form.
ReadOnly: Dữ liệu không thay đổi
ví dụ:
Ví dụ:
txtName.Focus();
String strName=txtName.Text;
Hoặc txtName.Text=“Value”;
II. Web Server Control II. Web Server Control
Button: thường sử dụng để submit form Checkbox
Phân loại: Các thuộc tính:
Button AutoPostBack
LinkButton Checked
ImageButton Text
Sự kiện Sự kiện:
Onclick() CheckedChange()
OnserverClick()
5
- 5/10/2013
II. Web Server Control II. Web Server Control
RadioButton Image:
Thuộc tính: Thuộc tính:
GroupName: tên của nhóm ImageUrl : Địa chỉ của hình cần hiển thị
Text: nội dung văn bản của radioButton AlternateText: Dòng văn bản hiển thị khi
Checked: radioButton được chọn hình không có sẳn
Sự kiện ImageAlign: canh vị trí tương đối của hình
onClick() so với văn bản trên trang
onCheckedChanged()
II. Web Server Control II. Web Server Control
Hyperlink Panel
Thuộc tính: Ý nghĩa: được dùng như ContainerControl
ImageUrl:đường dẫn đến hình cần hiển thị đối với các control khác, nó thi hành nhiều
(nếu dùng thuộc tính này thì hyperlink có tác chức năng:Kiểm soát các control chứa trong
dụng giống như Imagebutton) Panel Control
NavigateUrl: Địa chỉ URL cần link đến Được dẫn xuất từ lớp Webcontrol
Text: chuỗi văn bản chỉ mục liên kết hiển thị
trên trình duyệt
Target: chỉ cửa sổ hiển thị trang đích
6
- 5/10/2013
II. Web Server Control II. Web Server Control
Cú pháp Nhóm ListControl:
- 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 X X
DataValueField: thiết lập giá trị phần tử từ Chọn hơn 1 Item X X
DataSource Hiển thị toàn bộ danh
sách X
DataBind(): binding data vào ListControl
(Là các thông tin Data lấy được từ CSDL tại
Chương 7 )
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)
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 {
chkMonhoc.Items.Add(new ListItem(tenMH[i],maMH[i]));
lieu }
}
II. Web Server Control II. Web Server Control
Ví dụ: RadioButtonList protected void Page_Load(object sender, EventArgs e)
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()));
}
9
- 5/10/2013
II. Web Server Control II. Web Server Control
protected void radFont_SelectedIndexChanged(object sender, Table: Hiển thị thông tin dưới dạ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ộc tính tậ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
ImageUrl: URL của hình ảnh cần được hiển
ASP ControlHTML HTMLControl thị
NavigateUrl: URL của trang web phải
Table chuyển đến control khi có sự kiện click.
TableRow AlternateText: Dòng văn bản hiển thị khi
hình không có sẳn
TableCell
Keyword: loại quả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.
- 5/10/2013
Mã chương trình
V. Validation Server Control
2. Range Validator Server Control: Kiểm tra
Enter Your Name:
giới hạn nhập liệu, giá trị nhập phải nằm trong
khoảng giới hạn cho trước, giới hạn này có thể
control khác ở trên form để kiểm tra giới hạn
nhập liệu.
Display: có giá trị none, static, dynamic.
Chọn kiểu thể 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ểm tra dữ liêu nhập với khuôn biểu thức mẫu ValidationExpression: Khung của biểu thức
(RegularExpression) đã được định nghĩa trước. mẫu để so sánh kiểm tra
Visual Studio .NET cung cấp các khuôn biểu thức Cú pháp:
mẫu:
*
E-mail addresses
V. Validation Server Control V. Validation Server Control
4. CompareValidator Server Control: So sánh dữ Thuộc tính
liệu nhập với một trị trong một control khác hoặc ControlToCompare: chứa ID của một control
một hằng được cho trước khi thiêt kế hoặc một giá 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
>,>=,
- 5/10/2013
V. Validation Server Control V. Validation Server Control
Các sự kiện 5. CustomValidator Server Control
DataBinding() Kiểm tra tính hợp lệ dữ liệu của một control
Disposed() theo một yêu cầu, một ràng buộc nào đó, hay
Int()
một kiểu dữ liệu được người sử dụng định
nghĩa trước đó.
Load(): Lập trình sự kiện này là để đặt giá trị
CustomValidator Server Control có thể kiểm tra
ban đầu cho control.
hợp lệ cả phía client và server
PreRender()
Unload()
V. Validation Server Control V. Validation Server Control
Thuộc tính Các sự kiện
DataBinding()
ClientValidationFunction: thuộc tính này
nó chứa một tên hàm, mà hàm này được lập Disposed()
trình ở client (bằng javascript). Int()
ControlToValidate: Nhận ID của một Load(): Lập trình sự kiện này là để đặt giá trị
control trên form để kiểm tra dữ 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:
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ỗi từ tất cả các điều khiển
trên một trang
if (txtPIN.Text == "A999")
Cú pháp:
Value.IsValid = true;
else
}
16
- 5/10/2013
V. Validation Server Control V. Validation Server Control
Thuộc tính Các sự kiện
DisplayMode: cung cấp 3 định dạng hiển thị DataBinding()
Messagebox: List ,BulletList, SingleParagraph Disposed()
HeaderText: Dòng tiê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 : True thì 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ện thị control này
Unload()
khi chạy ứng dụng , False thì không (thường
dùng nhất.)
17
nguon tai.lieu . vn