HtmlTableRow
HtmlInputText
HtmlTextArea
HtmlInputButton
HtmlAnchor
HtmlInputCheckBox
HtmlButton
I.
HTML Server Control
Các s ki n (
onServerClick
onServerChange
onStartSelect,…
Cú pháp:
ủ : ự ệ event) c a HTML server Control
L u ýư
: Function không có tham số.
I.
HTML Server Control
Ví d :ụ
I.
HTML Server Control
ữ ệ ừ ấ
Cách l y d li u t các HTML server
Cú pháp:
controlfield_id.Value
controlfield_id.Value
Đ i v i tag DIV, SPAN dùng thu c tính
ố ớ ộ
.innerHTML
HTML Server Control
I.
Ví d :ụ
Ví dụ
II. Web Server Control
Web server control là nh ng tag đ c bi
ặ ệ ủ
ử t c a
c x lý trên server
ỏ ữ
ASP.NET. Các control này đ
ả
và đòi h i ph i có thu c tính ượ
runat= “server”
Web server control t n t
ộ
ồ ạ i bên trong không gian
Cú pháp:
tên System.Web.UI.WebControls
ệ ố ứ ậ ủ
H th ng th b c c a Web Server Control
System.Object
System.Web.UI.Control
Repeater
Xml
WebControl
AdRotator
LinkButton
Image
ListControl
BaseDataList
ImageButton
RadioButtonList
Label
DataGrid
CheckBoxList
BaseValidator
DataList
DropDownList
BaseCompareValidator
Button
ListBox
Calendar
CompareValidator
Panel
CheckBox
RangeValidator
Table
RadioButton
CustomValidator
TableCell
HyperLink
RegularExpressionValidator
TableHeaderCell
TextBox
RequiredFieldValidator
TableRow
ValidationSummary
II. Web Server Control
ơ ả
Nhóm control c b n
Cú pháp chung:
Các control c b n g m:
ơ ả ồ
II. Web Server Control
Label
Textbox
Button
CheckBox and Radio
Image, Hyperlink, Panel
List Controls group
Table
AddRotator
FileUpload
Validation
ể ả ị
II. Web Server Control
Label server control: dùng hi n th văn b n
ể ặ ậ
trên trình duy t.ệ
ộ
Thu c tính
:
ử ụ
Text: s d ng đ nh n ho c gán text
ví d : ụ
String strName=lblMsg.Text;
Or
lblMsg.Text=“Value”;
II. Web Server Control
ệ ừ ậ ườ ử ụ ng i s d ng
TextBox:dùng đ nh p li u t
và hi n th văn b n ch đ c
ỉ ọ ể
ả ể ị
ộ
Thu c tính :
ị
AutoPostBack: có 2 giá tr True và False khi
ộ ự
ộ
m t hành đ ng trên trang web b y m t s
ki n.ệ
ẩ ộ
Ví d : ụ
II. Web Server Control
TextMode: Lo i textbox: singleLine,
ạ
MultiLine, Password
Text: tr v giá tr ho c gán giá tr
ị
ReadOnly: D li u không thay đ i
ổ
ữ ệ
ả ề ặ ị
Ví d :ụ
ặ
String strName=txtName.Text;
Ho c txtName.Text=“Value”;
II. Web Server Control
ự ệ
S ki n:
Text_Changed()
Focus(): cho phép đ a tr v ph n t
ỏ ề ư ầ ử
ị ỉ c ch đ nh trên form.
ượ
đ
ví d : ụ
txtName.Focus();
II. Web Server Control
Button: th
ử ụ ể
ng s d ng đ submit form
ườ
Phân lo i:ạ
Button
LinkButton
ImageButton
S ki n
Onclick()
OnserverClick()
ự ệ
II. Web Server Control
Checkbox
Các thu c tính:
CheckedChange()
ộ
AutoPostBack
Checked
Text
S ki n:
ự ệ
II. Web Server Control
RadioButton
Thu c tính:
ộ
GroupName: tên c a nhóm
ủ
Text: n iộ dung văn b n c a radioButton
ủ ả
Checked: radioButton đ
ượ ọ c ch n
S ki n
onClick()
onCheckedChanged()
ự ệ
II. Web Server Control
Image:
ộ
Thu c tính:
ImageUrl : Đ a ch c a hình c n hi n th
ỉ ủ ể ầ ị ị
AlternateText: Dòng văn b n hi n th khi
ể ả ị
hình không có s nẳ
ị ươ ố ủ ng đ i c a hình
ImageAlign: canh v trí t
ả
so v i văn b n trên trang
ớ
II. Web Server Control
Hyperlink
ộ
Thu c tính:
ImageUrl:đ
ẫ ế ườ ể ầ
ế
ụ ố
NavigateUrl: Đ a ch URL c n link đ n
ỉ
Text: chu i văn b n ch m c liên k t hi n th
ị
ả
ị
ng d n đ n hình c n hi n th
ộ
(n u dùng thu c tính này thì hyperlink có tác
ư
d ng gi ng nh Imagebutton)
ầ
ị
ỉ ụ ế
ế ể ỗ
Target: ch c a s hi n th trang đích
trên trình duy tệ
ỉ ử ổ ể ị
II. Web Server Control
Panel
ư ượ
Ý nghĩa: đ
ố ớ
ứ
ề
ứ
ấ ừ ớ ượ ẫ c dùng nh ContainerControl
đ i v i các control khác, nó thi hành nhi u
ể
ch c năng:Ki m soát các control ch a trong
Panel Control
Đ c d n xu t t l p Webcontrol
II. Web Server Control
Cú pháp
ộ
Thu c tính
:
Visible: thu c tính nhìn th y đ
ựơ ủ ấ c c a control
ể ả ị ộ
bên trong Panel control
BackImageUrl: URL c a hình nh hi n th phía
ủ
sau table.
Backcolor: màu n n c a control
ủ ề
II. Web Server Control
Nhóm ListControl:
DropDownList
ListBox
CheckBoxList
RadioButtonList
Text=“text”>
Text=“text”>
II. Web Server Control
ạ ộ ớ ộ ố
ListItem: Ho t đ ng gi ng nhau v i 2 thu c tính
Value va Text
ể ượ
Item có th đ
ặ c thêm vào theo cách tĩnh
ậ
(Design) ho c L p trình (Coding), Add
ộ
Các thu c tính:
ả ề
SelectedIndex: tr v ch s c a ph n t
ỉ ố ủ ầ ử ượ đ c
ch n.ọ
ả ề
ả ề ọ
c ch n.
ọ
c ch n.
ầ ử ượ
đ
SelectedItem: tr v ph n t
ị ượ
SelectedValue: tr v giá tr đ
ự ệ
S ki n: SelectedIndexChaged
ặ
II. Web Server Control
AutoPostBack: true ho c false
Items.Count: tr v s ph n t
ả ề ố ầ ử trong listControls
Items.Add(ListItem): thêm ph n t
ầ ử vào listControls
Items.Remove(ListItem):
ListControl
xoá ầ
ph n t ử ỏ
kh i
Items.Clear(): Xoá t
ấ ả t c các ph n t ầ ử
.
ả ề ế ặ
Items[i].Selected: tr v true ho c false. True n u
ph n t
ớ
ầ ử ựơ ọ c ch n đ
V i i=0, n1 (n=Items.Count).
II. Web Server Control
DataSource: thi
ế ậ ị ừ ư
DataSource nh
t l p giá tr t
DataTable,DataSet,Array,Collection,DataView
DataTextField: thi
ế ậ ầ ử ừ t l p ph n t text t DataSource
DataValueField: thi
ế ậ ị t l p giá tr ph n t ầ ử ừ t
DataBind(): binding data vào ListControl
DataSource
ấ (Là các thông tin Data l y đ ượ ừ
c t CSDL t ạ
i
ươ Ch ng 7 )
II. Web Server Control
Các đặc tính
Checkbox
List
RadioButton
List
DropDown
List
ListBox
X
X
Chọn 1 Item duy nhất
X
X
Chọn hơn 1 Item
X
Hiển thị toàn bộ danh
sách
II. Web Server Control
Ví dụ: DropDownList
Anh
Pháp
Hoa
II. Web Server Control
protected void btnThem_Click(object sender,
EventArgs e)
{
drpLan.Items.Add(txtThem.Text);
}
protected void Page_Load(object sender, EventArgs e)
{
if(drpLan.SelectedItem!=null)
lblKq.Text="The selected item is: " +
drpLan.SelectedItem.Text;
else
lblKq.Text="No item is selected";
}
II. Web Server Control
Ví dụ: CheckBoxList
Cau hinh Phan
cung
Co so du
lieu
II. Web Server Control
protected void Page_Load(object sender, EventArgs e)
{
string[] maMH ={ "001","002","003","004"};
string[] tenMH ={"LT Windows", "LT
Internet","AVCN","Cau hinh Mang"};
for (int i = 0; i < maMH.Length; i++)
{
chkMonhoc.Items.Add(new ListItem(tenMH[i],maMH[i]));
}
}
II. Web Server Control
Ví dụ: RadioButtonList
10
12
14
16
II. Web Server Control
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int[] sizefont ={ 18, 20, 22, 24 };
for (int i = 0; i < sizefont.Length; i++)
radFont.Items.Add(new ListItem(sizefont[i].ToString()));
}
}
II. Web Server Control
protected void radFont_SelectedIndexChanged(object sender,
EventArgs e)
{
int sizeselected = Convert.ToInt32(radFont.SelectedValue);
lblKq.Font.Size = sizeselected;
}
II. Web Server Control
Table: Hi n th thông tin d
ể ị ướ ạ i d ng dòng và
c t. ộ
ự ả
ằ ậ ộ
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
Cú pháp
Width="100px" Height="70px">
ASP ControlHTML HTMLControl
Table
TableRow
TableCell
TableHeaderCell
II. Web Server Control
ả
AdRotator Server Control :qu ng cáo trên
trang web
ộ
Thu c tính:
ImageUrl: URL c a hình nh c n đ
ủ ả ầ ượ ể
c hi n
thị
NavigateUrl:
ể
ủ
ế URL c a
chuy n đ n control khi có s ki n click.
AlternateText: Dòng văn b n hi n th khi
ả
trang web ph i
ự ệ
ả ể ị
Keyword: lo i qu ng cáo
hình không có s nẳ
ả ạ
II. Web Server Control
ự
ệ
FileUpload Server Control: dùng th c hi n upload file lên
server
ộ
Các thu c tính:
c upload lên
ả
ố ượ
ứ
ủ
ộ
ư
ị ầ
ể
FileName: Tên file đ
ượ
FileBytes: M ng bytes ch a n i dung file upload
PostedFile: Hi n th đ y đ nh 1 đ i t
ng
ộ
ướ
ủ
c c a File.
ư ụ
ấ ỳ
ư
HttpPostedFile. HttpPostedFile có các thu c tính
FileName: Ten File
ạ
ContentType: Lo i File (.doc, .mdb, …)
ContentLength: Kích th
SaveAs: L u file upload vào 1 th m c b t k
II. Web Server Control
Ví dụ:
protected void btnShow_Click(object sender, EventArgs e)
{
lblFliename.Text =
FileUpload1.PostedFile.FileName.ToString();
lblType.Text =
FileUpload1.PostedFile.ContentType.ToString();
lblLength.Text =
FileUpload1.PostedFile.ContentLength.ToString();
//Luu file
string filename = FileUpload1.FileName.ToString();
FileUpload1.PostedFile.SaveAs(Server.MapPath("") +
"\\BT_Chuong3_ListControl" + filename);
}
II. Web Server Control
V. Validation Server Control
1. RequiredFieldValidator Server Control: yêu
ệ ả ậ ắ ộ i dùng b t bu c ph i nh p li u
ườ
ầ
c u ng
Cú pháp:
V. Validation Server Control
ộ
Thu c tính
ControlToValidate: nh n m t ID c a m t
ủ ậ
ộ
ậ ộ
ể ể 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.
ể ậ
ở script
ữ ệ ị
ộ
EnableClientScript: nh n true thì hi u các
phía client, false thì không.
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
Các s ki n
ể ặ ự ệ
DataBinding()
Disposed()
Int()
Load(): L p trình s ki n này là đ đ t giá tr
ị
ự ệ
PreRender()
Unload()
ầ ậ
ban đ u cho control.
V. Validation Server Control
Ví d : thi
ụ ư t k form có d ng nh sau, khi click
ư ệ ấ ế ế
ế ạ
ậ
nút submit, n u ch a nh p tên thí xu t hi n
thông báo l iỗ
ươ
ng trình
Mã ch
V. Validation Server Control
2. Range Validator Server Control: Ki m tra gi
ạ
ả
ớ ạ ệ
ớ ạ ậ
c, gi
ế ế ượ
ể
ớ
i
ằ
ị
ậ
h n nh p li u, giá tr nh p ph i n m trong
ể
ướ
ả
kho ng gi
i h n này có th
i h n cho tr
ặ
ư
ằ
t k ho c so
c đ a vào lúc thi
là các h ng đ
ớ
sánh v i các control khác trên trang web
V. Validation Server Control
Cú pháp:
minimumValue=minValue maximumValue=maxValue>
V. Validation Server Control
minimumValue, maximumValue: kho ng ả
ệ
ộ
Thu c tính:
ớ ạ ậ ị
i h n giá tr nh p li u gi
ControlToValidate: ch a ID c a m t control
ứ
ể ể
i h n nh p
ộ
ớ ạ ậ ở ủ
trên form đ ki m tra gi
Display: có giá tr none, static, dynamic.
ị
khác
li u. ệ
ể ọ ể ệ
Ch n ki u th hi n thông báo.
V. Validation Server Control
ể ữ ứ ậ
ị ể
c đ nh nghĩa tr
ể
3. RegularExpressionValidator Server Control:
ẫ
ớ
ki m tra d liêu nh p v i khuôn bi u th c m u
ướ
ượ
c.
(RegularExpression) đã đ
ứ
ấ
Visual Studio .NET cung c p các khuôn bi u th c
m u:ẫ
(cid:0) Telephone numbers
(cid:0) Postal codes
(cid:0) Email addresses
V. Validation Server Control
Thu c tính:
ộ
ứ ủ ể
ể ể ValidationExpression: Khung c a bi u th c
m u đ so sánh ki m tra
ẫ
Cú pháp:
ControlToValidate=“Input_control"…
ControlToValidate=“Input_control"…
ValidationExpression =“RegularExpression "…>*
ValidationExpression =“RegularExpression "…>*
V. Validation Server Control
ậ ặ ộ
ướ ế ặ ộ ị
ộ
c cho tr
ớ
ượ
ữ ệ
4. CompareValidator Server Control: So sánh d ữ
ệ
li u nh p v i m t tr trong m t control khác ho c
ộ ằ
c khi thiêt k ho c m t giá
m t h ng đ
ị
tr trong d li u. Các phép toán so sánh >,>=,<,<=
Cú pháp:
type="Double" operator="DataTypeCheck">
type="Double" operator="DataTypeCheck">
V. Validation Server Control
ộ
Thu c tính
ứ ủ ộ
ẽ
ControlToCompare: ch a ID c a m t control
ớ ữ
mà giá tr c a control ID này s so sánh v i d
ệ
li u c a m t control khác
ị ủ
ộ ủ
ộ ứ
ControlToValidate: ch a ID c a m t control
c so sánh v I
ủ ủ
ượ ớ
ữ ệ ộ thu c tính trên.
ể ồ
ữ ệ
mà d li u c a control này đ
ủ
ở
d li u c a control
Operator: Toán t
ử
so sánh
Type : Ki u d li u đ so sánh g m (String,
ể
ữ ệ
Integer, Date, Double, Currency)
ValueToCompare: ch a giá tr so sánh h ng
ứ ằ ị
V. Validation Server Control
Các s ki n
ể ặ ự ệ
DataBinding()
Disposed()
Int()
Load(): L p trình s ki n này là đ đ t giá tr
ị
ự ệ
PreRender()
Unload()
ầ ậ
ban đ u cho control.
V. Validation Server Control
ể ộ
ợ
ầ
ữ ệ ử ụ ườ ộ ị c ng
5. CustomValidator Server Control
ủ
ệ ữ ệ
Ki m tra tính h p l
d li u c a m t control
ộ
ộ
ộ
theo m t yêu c u, m t ràng bu c nào đó, hay
ượ
ể
m t ki u d li u đ
i s d ng đ nh
ướ
nghĩa tr
c đó.
CustomValidator Server Control có th ki m tra
ể ể
ợ ệ ả h p l c phía client và server
V. Validation Server Control
ộ
Thu c tính
ộ
ClientValidationFunction: thu c tính này nó
c l p
ượ ậ ộ
ằ ở ứ
ch a m t tên hàm, mà hàm này đ
client (b ng javascript).
trình
ControlToValidate: Nh n
ậ ộ
ID c a m t
ữ ệ ể ể ủ
control trên form đ ki m tra d li u.
V. Validation Server Control
Các s ki n
ể ặ ự ệ
DataBinding()
Disposed()
Int()
Load(): L p trình s ki n này là đ đ t giá tr
ị
ự ệ
ầ ậ
ban đ u cho control.
PreRender()
ServerValidate(): s ki n này đ
ể ể
ự ệ
ượ ậ
ợ ệ ủ c l p trình
ữ
c a d
trên server đ ki m tra tính h p l
li u.ệ
Unload()
V. Validation Server Control
ế ế ư ể ầ ố ụ
Ví d : thi t k form nh sau, yêu c u ki m tra s Pin
//Mã giao di nệ
User ID:
PIN:
OnClick="SaveBtn_Click" runat=server />
ControlToValidate="txtPIN"
OnServerValidate="ServerVerify" runat="server">
Invalid PIN number!
ể //hàm ServerVerify ki m tra trên server
void ServerVerify(Object Sender,
ServerValidateEventArgs Value)
{
if (txtPIN.Text == "A999")
Value.IsValid = true;
else
Value.IsValid = false;
}
V. Validation Server Control
ợ
ậ
ể
6. ValidationSummary Server Control: t p h p
ề
ỗ ừ ấ ả
t c các đi u khi n trên
t
i t
các thông báo l
ộ
m t trang
Cú pháp:
ShowSummary="True" DisplayMode="List"/>
V. Validation Server Control
ộ
Thu c tính
ể ạ ị
ủ ề
DisplayMode: cung c p 3 đ nh d ng hi n th
ị
ấ
Messagebox: List ,BulletList, SingleParagraph
HeaderText: Dòng tiêu đ cho thông báo c a
các control.
ShowMessageBox : True thì hi n thông báo
ệ
ộ ệ
đ ng, False thì hi n thông báo tĩnh.
ệ ị
ShowSummary: True là hi n th control này
ng
ườ ụ
ạ ứ
khi ch y ng d ng , False thì không (th
dùng nh t.)ấ
V. Validation Server Control
Các s ki n
ể ặ ự ệ
DataBinding()
Disposed()
Int()
Load(): L p trình s ki n này là đ đ t giá
PreRender()
Unload()
ậ
ầ ị ự ệ
tr ban đ u cho control.