Web Form Controls
Controls ត្រូវបានបត្រើសត្ារ់ប ៀរចំ interface សត្ារ់
Web application មួយ។
ASP.NET ផ្តល់ឲ្យបយើងនូវសំណំ នន standard controls
បៅក្នង Toolbox រស់ Visual Studio ដែលអាចត្រូវបាន
បត្រើសត្ារ់កា ប ៀរចំ Web pages។
ត្ររ់ standard/Web server controls អាស្សួយបៅបលើ
WebControl class។
ហោ សូ ហនឿនWeb Server Controls
2
3.
Basic Property
Name
ជាproperty សត្ារ់
ក្ំណរ់ប្ម ោះឲ្យ control។
Enabled, Locked , ReadOnly
បែើមបីក្ំណរ់ឲ្យ control
មួយានសមរថភាពត្រឹមដរ
រង្ហា ញទិននន័យមិនអនញ្ញា រ
ឲ្យអនក្បត្រើដក្ដត្រ ឺរញ្ចូ ល
ទិននន័យ ឺចច បៅបលើ
control បនោះបាន។
ហោ សូ ហនឿនWeb Server Controls
3
The Button Control
ជា control មួយបត្រើសត្ារ់រញ្ជូ ន request បៅបលើ browser
បៅកាន់ server។
ជាធមមតាវាានត្របេទជា “Submit”។
បៅបពលដែលបយើងចចបលើ button, control sទាំងអស់ដែល
សថិរបៅក្នង form ដែលបយើងបានចច button ប ោះនឹងត្រូវបាន
រញ្ជូ នបៅកាន់ server សត្ារ់អនវរតកា ង្ហ ។
បែើមបី ក្់រញ្ចូ ល Button បៅបលើ web page បយើងអាចបត្រើ tag
ែូចខាងបត្កាម:
<asp:Button ID=“…” text =“OK” runat=“Server”></asp:Button>
ហោ សូ ហនឿនWeb Server Controls
18
19.
Button Control
បែើមបីសបស code ឲ្យ Button មួយបយើងត្រូវ double
click បៅបលើ button ប ោះ។
Code បនោះត្រូវបានបៅថា behind-code។
Protected Sub Button1_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Button1.Click
Statements;
End Sub
ហោ សូ ហនឿនWeb Server Controls
19
20.
The DropDownList Control
DropDownList រឺជាលក្ខណៈនន TextBox ដែលានទិននន័យ
ស្សារ់សត្ារ់ឲ្យអនក្បត្រើបត្ជើសប ើស។
បែើមបី ក្់រញ្ចូ ល Button បៅបលើ web page បយើងអាចបត្រើ tag
ែូចខាងបត្កាម:
<asp:DropDownList ID=“Name" runat="server">
………………..
………………..
</asp:DropDownList>
ហោ សូ ហនឿនWeb Server Controls
20
Value Property
វាត្រូវបានក្ំណរ់បៅក្នង<asp:ListItem > .. </asp:ListItem>
tag បែើមបីក្ំណរ់រនមាសត្ារ់រង្ហា ញបៅក្នង DropDownList
និងកា ទាញយក្រនមាពី DropDownList ។
<asp:DropDownList ID=“sex” runat="server">
<asp:ListItem value=female>Female</asp:ListItem>
<asp:ListItem value=male>Male</asp:ListItem>
</asp:DropDownList>
Dim V1
V1=Request(“sex”)=“female”
sex.Text=female
Retrieving Text
Displaying Text
ហោ សូ ហនឿនWeb Server Controls
23
24.
Retrieving Items
បែើមបីទាញយក្រនមាពី DropDownListបយើងអាចបត្រើ method៖
1. SelectedItem: ទាញយក្រនមាជា text ពី DropDownList។
2. SelectedIndex: ទាញយក្រនមាជា index ពី DropDownList។
3. SelectedValue : ទាញយក្រនមាជា value ពី DropDownList។
Dim a
a=sex.SelectedItem.Text.ToString
Dim a
a=sex.SelectedIndex.ToString
Dim a
a=sex.SelectedItem.Text.ToString
ហោ សូ ហនឿនWeb Server Controls
24
SelectIndexChanged Event
ជាevent រស់ CheckBoxList ដែលបក្ើរប ើងបពលអនក្បត្រើ
Select បៅបលើ Item ណមួយ រស់ CheckBoxkList។
Protected Sub ChkTime_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ChkTime.SelectedIndexChanged
If ChkTime.SelectedValue = "m" Then
Response.Write("Good Morning")
If ChkTime.Items.Item(1).Selected = True Then
Response.Write("Good Afternoon")
End If
Else
If ChkTime.Items.Item(1).Selected = True Then
Response.Write("Good Afternoon")
End If
End If
End Sub
ហោ សូ ហនឿនWeb Server Controls
32
The Table Control
ត្រូវបានបត្រើបែើមបីរបងកើរ Table បៅបលើ Web page។
BackImageUrl : To display the background image of the table.
Caption : To specify the title of the table.
CellPadding : To set the margin of the cell in the table.
CellSpacing : To set the space between cells.
GridLines : To display the table in grid line style.
Rows : To retrieve the numbers of rows in the table.
<asp:Table BorderStyle=Solid BorderColor=Red ID=tbl1 runat=server Width=100%>
</asp:Table>
ហោ សូ ហនឿនWeb Server Controls
44
Formatting Tables
Someuseful properties of the TableRow:
Cells : To get the numbers of cells in the row.
HorizontalAlign : To the alignment of the row in the table, left,
right, or center.
VerticalAlign : To the alignment of the row in the table, top,
middle, or buttom.
Some useful properties of the TableCell:
ColumnSpan : To widen the side of cells compare to columns.
RowSpan : To widen the side of cells compare to rows.
HorizontalAlign : To the alignment of the cell in the table, left,
right, or center.
VerticalAlign : To the alignment of the cell in the table, top,
middle, or buttom.
Text : To get/set the text in the cell.
ហោ សូ ហនឿនWeb Server Controls
47
48.
The Calendar Control
រង្ហា ញកាលរ ិបចេទជាទត្មង់ calendar។
អនញ្ញា រឲ្យអនក្របត្រើអាចបត្ជើសប ើសយក្កាលរ ិបចេទជា
ទត្មង់ Calendar។
By default, It displays
The days of the month
The day headings for the
days of the week
A title with the name of
the month
And the navigation links to
the next or previous month.
<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
ហោ សូ ហនឿនWeb Server Controls
48
Calendar Control
Exampleyou have and .aspx page for calendar as
below:
<form id="form1" runat="server">
<div>
<asp:Calendar ID="Calendar1" runat="server" ForeColor=Black
CellPadding =2 CellSpacing=2>
<TitleStyle BackColor=Blue Font-Bold=true />
<SelectedDayStyle Font-Italic=true ForeColor=Red />
<WeekendDayStyle BackColor=red ForeColor=blue />
<TodayDayStyle BackColor=Gray ForeColor=DarkCyan />
</asp:Calendar>
<asp:Label ID="Label1" runat="server" Text="Selected Day"></asp:Label>
<asp:TextBox ID="txtSelDay" runat="server"></asp:TextBox>
</div>
</form>
ហោ សូ ហនឿនWeb Server Controls
50
51.
Calendar Event
Thenyou can write the behind code (VB Code) for the
calendar as below:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
'Calendar1.SelectedDate = DateAndTime.Now
Calendar1.SelectedDate = DateAndTime.DateString
txtSelDay.Text = Calendar1.SelectedDate()
End Sub
Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Calendar1.SelectionChanged
txtSelDay.Text = Calendar1.SelectedDate()
End Sub
End Class
ហោ សូ ហនឿនWeb Server Controls
51
52.
HyperLink Control
Itis used to create a link to another Web page.
With the “HyperLink” control, you can create:
A text link (Using “Text” property)
Or an image link. (Using “ImageUrl” property)
In the “HyperLink” control you may need to set the
properties such as:
ImageUrl : To display the image that will be a Link.
NavigateUrl : To specify where the Link will navigate to.
Target : The set which frame or window the navigated page will be
displayed.
Text : To set the text to be display as a Link.
ហោ សូ ហនឿនWeb Server Controls
52
53.
HyperLink Control
Seethe Example:
<form id="form1" runat="server">
<div>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="http://www.w3school.com"
ImageUrl="~/PDVD_072 copy.jpg">
Go to w3Schools
</asp:HyperLink>
</div>
</form>
ហោ សូ ហនឿនWeb Server Controls
53
54.
FileUpload Control
Itis used for a user upload a file from a client side to
the server side.
Important Properties:
FileName : Provides the name of the file on the client computer
that will be uuloaded.
HasName : Is used to make sure if the upload control contain any
file or not?
Brows to a file you
want to upload
ហោ សូ ហនឿនWeb Server Controls
54
55.
FileUpload Control
See theExample:
You have a form with the file upload control:
<form id="form1" runat="server">
<asp:Label id=lbl1 runat=server Text="Please enter the text to upload">
</asp:Label>
<br />
<asp:FileUpload ID=upload1 runat=server /> </br>
<asp:Button ID=btn1 runat=server Text=Upload /> <br />
<asp:Label ID=lbl2 runat=server></asp:Label>
</form>
ហោ សូ ហនឿនWeb Server Controls
55
56.
FileUpload Control
Thenhere is the vb code for the upload button:
Protected Sub btn1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btn1.Click
If upload1.HasFile Then
upload1.SaveAs("D:asp.netChapter4" &
upload1.FileName)
lbl2.Text = "File Uloaded"
Else
lbl2.Text = "No File Upload"
End If
End Sub
ហោ សូ ហនឿនWeb Server Controls
56