Transcript Slide 1

ทส215
การเขียนโปรแกรมบนเว็บ 1
Server Control
ั
อาจารย์อรรถวิท ชงคมานนท์
สาขาวิชาเทคโนโลยีสารสนเทศ
คณะวิทยาศาสตร์
www.itsci.mju.ac.th
1
[email protected]
IIS + Application Domain
2
[email protected]
3
[email protected]
4
[email protected]
5
[email protected]
การประกาศใช ้ Server control
 วิธก
ี ารประกาศตัวแปรลงใน Web form โดยการระบุ tag ดังนี้
<asp:ชนิดของคอนโทรล attributes… />
หรือ
<asp:ชนิดของคอนโทรล attributes… ></asp:ชนิดของคอนโทรล>
่
เชน
<asp:button ID=“Button1” runat=“server” text=“Enter” Onclick=“EnterBtn_Click” />
<asp:TextBox id="TextBox1" runat="server" Width="202"></asp:TextBox>
6
[email protected]
ต ัวอย่างของ Server control
Server Control
้ าหร ับ
ใชส
Tag HTML
<asp:Button …/>
แสดงปุ่ มกด
<input type=“submit” …>
<asp:TextBox …/>
่ งกรอกข ้อมูล
ชอ
<input type=“text” …>
<asp:Label …/>
แสดงข ้อความ
<span>…</span>
<asp:CheckBox …/>
เลือกรายการได ้มากกว่า 1
รายการ
<input type=“checkbox” …>
<asp:RadioButton …/>
เลือกรายการได ้เพียง 1
รายการ
<input type=“radio” …>
<asp:ListBox …/>
แสดงรายการให ้เลือกและ
แสดงรายการให ้ดูตาม size
ทีร่ ะบุ
<select size=“5” … >
<option>…..</option>
</select>
7
[email protected]
ต ัวอย่างของ Server control
Server Control
<asp:CheckBoxList …/>
้ าหร ับ
ใชส
เลือกรายการได ้มากกว่า 1
รายการ
Tag HTML
<input
type=“checkbox” …>
<asp:RadioButtonList …/> เลือกรายการได ้เพียง 1
รายการ
<input type=“radio” …>
<asp:DropDownList …/>
แสดงรายการให ้เลือกและ
แสดงรายการให ้ดูเพียง 1
รายการ
<select …>
แสดงรูปภาพ
<img … >
<asp:Image …/>
8
<option>…..</option>
</select>
[email protected]
Button
 Attributes ทีน
่ ่าสนใจของ Button
ID=“…”
Text=“…”
Width=200px
Runat=“server”
OnClick=“…”
่
 เชน
<asp:Button id=“Btn1” runat=“server” Text="Click !!"
Width=“105px” Height=“33px”></asp:Button>
9
[email protected]
TextBox และ Label
 Attributes ทีน
่ ่าสนใจของ TextBox
ID=“…”
Runat=“server”
TextMode=“…” สามารถกาหนดได ้ดังนี้ Password, SingleLine, MultiLine
้
MaxLength=20 ใชในกรณี
ทเี่ ป็ น SingleLine
Column=30 จานวนอักขระในแต่ละบรรทัด กรณีเป็ น MultiLine
Rows=5 จานวนบรรทัด กรณีเป็ น MultiLine
OnTextChanged เป็ นเหตุการณ์เมือ
่ มีการเปลีย
่ นแปลงข ้อความใน TextBox
10
[email protected]
TextBox และ Label
่
 เชน
<asp:TextBox id="TextBox2" runat="server" Width="202px"
Height="63px" TextMode="MultiLine" Rows="3" Columns="25">
</asp:TextBox>
<asp:Label id="Label1" runat="server" Width="76px"
Height="19px" >Address</asp:Label>
11
[email protected]
CheckBox
 Attributes ทีน
่ ่าสนใจของ CheckBox
ID=“…”
Runat=“server”
Checked
ิ ด ้านใดด ้านหนึง่
TextAlign การจัดข ้อความให ้ชด
็
OnCheckChange=“…” เหตุการณ์เมือ
่ มีการเปลีย
่ นแปลงการเชค
่
 เชน
<asp:CheckBox id="CheckBox3" runat="server" Width="160px"
Height="21px“ Text="ASP"></asp:CheckBox>
12
[email protected]
CheckBoxList
 Attributes ทีน
่ ่าสนใจของ CheckBoxList
ID=“…”
Runat=“server”
่
 เชน
<asp:CheckBoxList id="CheckBoxList1" runat="server"
Width="142px" Height="113px">
<asp:ListItem Value="bbbbb" Selected="True">bbbbb</asp:ListItem>
<asp:ListItem Value="ccccc">ccccc</asp:ListItem>
<asp:ListItem Value="ddddd">ddddd</asp:ListItem>
</asp:CheckBoxList>
13
[email protected]
RadioButton
 Attributes ทีน
่ ่าสนใจของ RadioButton
ID=“…”
Runat=“server”
Checked
GroupName เป็ นการกาหนดกลุม
่ ให ้กับ RadioButton
็
OnCheckChange=“…” เหตุการณ์เมือ
่ มีการเปลีย
่ นแปลงการเชค
่
 เชน
<asp:RadioButton id="RadioButton1" runat="server" Text="ASP"
Width="165px" Height="30px“ GroupName="Skill"> </asp:RadioButton>
14
[email protected]
RadioButtonList
 Attributes ทีน
่ ่าสนใจของ RadioButtonList
ID=“…”
Runat=“server”
่
 เชน
<asp:RadioButtonList id="RadioButtonList1" runat="server" Width="97px"
Height="71px">
<asp:ListItem Value="aaa" Selected="True">aaa
</asp:ListItem>
<asp:ListItem Value="bbb">bbb</asp:ListItem>
<asp:ListItem Value="ccc">ccc</asp:ListItem>
</asp:RadioButtonList>
15
[email protected]
Image
 Attributes ทีน
่ ่าสนใจของ Image
ID=“…”
Runat=“server”
้ บรูป
ImageUrl ตาแหน่งทีใ่ ชเก็
์ าวางเหนือรูป
AlternateText ข ้อความเมือ
่ เลือ
่ นเมาสม
่
 เชน
<asp:Image id="Image1" runat="server" Width="142px" Height="139px"
ImageUrl="\Blue_hills.jpg"></asp:Image>
16
[email protected]
DropDownList
 Attributes ทีน
่ ่าสนใจของ DropDownList
ID=“…”
Runat=“server”
่
 เชน
<asp:DropDownList id="DropDownList1" runat="server"
Height="34px" Width="155px">
<asp:ListItem Value="aaaaaa">aaaaaa</asp:ListItem>
<asp:ListItem Value="bbbbb">bbbbb</asp:ListItem>
<asp:ListItem Value="cccccc">cccccc</asp:ListItem>
</asp:DropDownList>
17
[email protected]
ListBox
 Methods ทีน
่ ่าสนใจของ
Items[2]
Items.Add
Items.Insert
Items.Remove
Items.Clear
ListBox
แสดงค่าใน List ลาดับที่ 3
เพิม
่ รายการเข ้าไปใน List
แทรกรายการเข ้าไปใน List
ลบรายการออกจาก List
ลบรายการออกจาก List ทัง้ หมด
 Properties ทีส
่ าคัญของ
SelectedIndex
SelectedItem
Items.Count
ListBox
ลาดับของรายการใน List โดยเริม
่ จาก 0
รายการทีเ่ ลือก
นั บจานวนรายการใน List
18
[email protected]
ต ัวอย่างการใช ้ ListBox
่
 เชน
ListBox1.Items.Add(“aaaaaaaaa”);
ListBox1.Items.Remove(“aaaaaaaaa”);
หรือ
ListBox1.Items.Remove(ListBox1.SelectedItem.Value)
ListBox1.Items.Clear
ListBox1.Items.Insert(2,“aaaaaaaaa”) //โดยที่ 2 คือลาดับทีต
่ ้องการแทรกข ้อมูล
19
[email protected]
ต ัวอย่างการสร้าง Web Form
โดยใช ้ Server Controls
20
[email protected]
ต ัวอย่างการสร้าง Web Form (.aspx)
<%@Page language="C#"%>
<html>
<head><title>Server Control</title></head>
<body>
<form action="example1.aspx" runat="server">
Name : <asp:TextBox id="TextBox1" runat=“server”></asp:TextBox><br>
Address : <asp:TextBox id="TextBox2" runat="server" TextMode="MultiLine"
Rows="3" Columns="25"></asp:TextBox><br>
Language :
<asp:CheckBoxList id="CheckBoxList1" runat="server" >
<asp:ListItem Value="ASP" Selected="True"> ASP</asp:ListItem>
<asp:ListItem Value="JSP">JSP</asp:ListItem>
<asp:ListItem Value="HTML">HTML</asp:ListItem>
</asp:CheckBoxList>
(ต่อ) 
21
[email protected]
ต ัวอย่างการสร้าง Web Form (.aspx)
Do you like computer ?
<asp:RadioButtonList id="RadioButtonList1" runat="server" >
<asp:ListItem Value="1" Selected="True">Yes</asp:ListItem>
<asp:ListItem Value="0">No</asp:ListItem>
</asp:RadioButtonList>
Your country :
<asp:DropDownList id="DropDownList1" runat="server" >
<asp:ListItem Value="THA">Thailand</asp:ListItem>
<asp:ListItem Value="USA">America</asp:ListItem>
<asp:ListItem Value="ENG">england</asp:ListItem>
</asp:DropDownList><br>
<asp:button text="Enter" runat="server" ID="Button1” OnClick="Button1_Click" />
<asp:Label
<asp:Label
<asp:Label
<asp:Label
<asp:Label
</form>
</body>
</html>
id="Label1"
id="Label2"
id="Label3"
id="Label4"
id="Label5"
runat="server"/><br>
runat="server"/><br>
runat="server"/><br>
runat="server"/><br>
runat="server"/>
22
[email protected]
ต ัวอย่างการสร้าง Web Form (.aspx.cs)
private void Button1_Click(object sender, System.EventArgs e){
string s = "";
Label1.Text = "Hello " + TextBox1.Text;
Label2.Text = "Address : " + TextBox2.Text;
for (int i = 0;i< CheckBoxList1.Items.Count ; i++) {
if (CheckBoxList1.Items[i].Selected) {
s = s + CheckBoxList1.Items[i].Text + “ ” ;
}
}
Label3.Text = "<br>Language : " + s;
if (RadioButtonList1.SelectedItem.Value == "0")
Label4.Text = "You don't like computer";
else
Label4.Text = "You like computer";
}
Label5.Text = "You live in " + DropDownList1.SelectedItem.Text;
23
[email protected]
่ ข้อมูลไปย ังหน้าอืน
การสง
่ Server Control
 ในการสง่ ข ้อมูลของ Form แบบ Server control ให ้กาหนด Properties
PostBackUrl="~/Default2.aspx" ในปุ่ มทีท
่ าการสง่ ข ้อมูล
 ตัวอย่างการรับข ้อมูล
Response.Write( ((TextBox)PreviousPage.FindControl("TextBox1")).Text );
ประเภทของ Server Control
ื่ ของ Control
ชอ
24
[email protected]
Server Controls Validation
25
[email protected]
Server Controls Validation
 เป็ นการตรวจสอบรูปแบบการกรอกข ้อมูลของผู ้ใชว่้ าถูกต ้องตามรูปแบบหรือไม่
Server Control
ความหมาย
RequiredFieldValidator
่ ้อมูลลงใน control นีเ้ สมอ
ต ้องใสข
CompareValidator
เปรียบเทียบข ้อมูล 2 ตัว
RangeValidator
่ ้อมูลในชว่ งทีก
ต ้องใสข
่ าหนดเท่านัน
้
RegularExpressionValidator
่ email เบอร์โทรศัพท์ หรือ
ใชก้ าหนดรูปแบบเฉพาะเชน
รหัสไปรษณีย ์
CustomValidator
ผู ้ใชก้ าหนดรูปแบบการตรวจสอบขึน
้ มาเอง
ValidationSummary
้
ใชการตรวจสอบหลาย
ๆ อย่างรวมกัน
26
[email protected]
Server Controls Validation
 รูปแบบทั่วไป
<asp:ประเภทของ Validator
ื่ ”
ID=“ชอ
ErrorMessage=“ข ้อความเมือ
่ เกิดข ้อผิดพลาด”
ื่ server control ทีต
ControlToValidate=“ชอ
่ ้องการตรวจสอบ”
runat=“server” />
27
[email protected]
RequiredFieldValidator
28
[email protected]
RequiredFieldValidator
<%@Page language="C#"%>
<html>
<head><title>Required Field Validator</title></head>
<body>
<form action="example2.aspx" runat="server">
User Name :
<asp:textbox id="txtusername" runat="server" Maxlength=20 />
<asp:RequiredFieldValidator id="check1" ErrorMessage="** required !!!"
ControlToValidate="txtusername" runat="server" /><br>
Password :
<asp:textbox id="txtpassword" runat="server" Maxlength=10 TextMode="Password" />
<asp:RequiredFieldValidator id="check2" ErrorMessage="** required !!!"
ControlToValidate="txtpassword" runat="server" /><br>
<asp:button id="submit" runat="server" text="log in" /><br>
</form>
</body>
</html>
29
[email protected]
CompareValidator
 Attributes ทีส
่ าค ัญ
ControlToValidate
ControlToCompare
คอนโทรลทีเ่ ก็บค่าแรก
คอนโทรลทีเ่ ก็บค่าทีจ
่ ะเปรียบเทียบ
Type
ประเภทการเปรียบเทียบ
String Double Date Integer Currency
Operator
เครือ
่ งหมายในการเปรียบเทียบ
Equal
เท่ากับ
NotEqual
ไม่เท่ากับ
GreaterThan
มากกว่า
GreaterThanEqual
มากกว่าหรือเท่ากับ
LessThan
น ้อยกว่า
LessThanEqual
น ้อยกว่าหรือเท่ากับ
30
[email protected]
CompareValidator
31
[email protected]
CompareValidator
<%@Page language="C#"%>
<html>
<head><title>Compare Validator</title></head>
<body>
<form action="example3.aspx" runat="server">
text validate : <asp:textbox id="txtvalid" runat="server" /><br>
text compare : <asp:textbox id="txtcompare" runat="server" />
<asp:CompareValidator id="check1"
ErrorMessage="Not Equal !!!"
ControlToValidate="txtvalid" ControlToCompare="txtcompare"
Type="String" Operator="Equal"
runat="server" />
<br>
<asp:button id="submit" runat="server" text="Compare" /><br>
</form>
</body>
</html>
32
[email protected]
RangeValidator
 Attributes ทีส
่ าคัญ
MinimumValue
MaximumValue
Type
ค่าเริม
่ ต ้น
ค่าสุดท ้าย
ประเภทการเปรียบเทียบ
String Double Date Integer Currency
33
[email protected]
RangeValidator
34
[email protected]
RangeValidator
<%@Page language="C#"%>
<html>
<head><title>Range Validator</title></head>
<body>
<form action="example4.aspx" runat="server">
Integer 1-100 : <asp:textbox id="txtint" runat="server" />
<asp:RangeValidator id="check1" ErrorMessage="Not Valid !!!"
ControlToValidate="txtint" runat="server" Type="Integer"
MinimumValue="1" MaximumValue="100"/><br>
Date 1/1/2004 - 31/12/2005 : <asp:textbox id="txtdate" runat="server" />
<asp:RangeValidator id="check2" ErrorMessage="Not Valid !!!"
ControlToValidate="txtdate" runat="server" Type="Date" MinimumValue="1/1/2004"
MaximumValue="31/12/2005"/><br>
String A - F : <asp:textbox id="txtString" runat="server" />
<asp:RangeValidator id="check3" ErrorMessage="Not Valid !!!"
ControlToValidate="txtString" runat="server" Type="String"
MinimumValue="A" MaximumValue="F"/><br>
<asp:button id="submit" runat="server" text="Compare" /><br>
</form>
</body>
</html>
35
[email protected]
RegularExpressionValidator
ั
สญล
ักษณ์
ความหมาย
?
้
่ An? = An And Ann …
ใชแทนตั
วอักษรไม่เกิน 1 ตัว เชน
+
้
่ An+ = And Ann Analog …
ใชแทนตั
วอักษรอย่างน ้อย 1 ตัว เชน
.
้
่ An. = And Ann Ant …
ใชแทนตั
วอักษร 1 ตัวเท่านัน
้ เชน
*
้
่ An* = An And Ann Analog …
ใชแทนตั
วอักษรกีต
่ ัวก็ได ้ เชน
\
ใชน้ าหน ้าตัวอักษรพิเศษ ได ้แก่ . $ ^ { } [ ] ( ) | * + ? \
[a-z] หรือ [0-9]
่ [b-n]ook = book
ใชก้ าหนดชว่ งตัวอักษรหรือตัวเลข ทีต
่ ้องการค ้นหา เชน
look hook …
[abcd…]
่ b[aeiou]nd = band bend
ใชก้ าหนดเซตของตัวอักษรทีต
่ ้องการ เชน
bind bond …
[^abcd…]
่ b[^io]nd = band bend …
ใชก้ าหนดเซตของตัวอักษรทีต
่ รงกันข ้าม เชน
แต่ไม่ใช ่ bind bond
36
[email protected]
RegularExpressionValidator
ั
สญล
ักษณ์
ความหมาย
a{n}
่ w{3} = www หรือ
มีข ้อความทีก
่ าหนดปรากฏจานวน n ครัง้ เชน
[0-9]{5} = มีตัวเลข 5 ตัว
a{n,}
่ w{3,} = www
มีข ้อความทีก
่ าหนดปรากฏจานวน n ครัง้ ขึน
้ ไป เชน
หรือ wwwww
a{n,m}
มีข ้อความทีก
่ าหนดปรากฏจานวน n ครัง้ ขึน
้ ไปแต่ไม่เกิน m
\d
เทียบเท่ากับ [0-9]
\D
่ ัวเลข
เทียบเท่ากับ [^0-9] หรือ ไม่ใชต
\w
ต ้องเป็ นอักษรหรือตัวเลข
\W
ตรงข ้ามกับ \w
\s
่ งว่าง
แทนชอ
\S
่ อ
่ งว่าง
ไม่ใชช
37
[email protected]
RegularExpressionValidator
ั
สญล
ักษณ์
ความหมาย
^
ต ้องขึน
้ ต ้นด ้วย String
$
ิ้ สุดด ้วย String
ต ้องสน
(exp1|exp2|…)
่ \.(com|net|org)
กาหนดรูปแบบใด ๆ เชน
• ตัวอย่าง
• รหัสไปรษณีย ์ “\d[0-9]{5}” ต ้องเป็ นตัวเลข และ มีจานวน 5 ตัว
ั ท์ (xx) xxx-xxxx = “\(0[0-9]{1}\)\s[0-9]{3}-[0-9]{4}”
• เบอร์โทรศพ
(xxx) xxx-xxx = “\(0[0-9]{2}\)\s[0-9]{3}-[0-9]{3}”
• Email [email protected]
= “[\w-]+@[\w-]+\.(com|net|org|co\.th|ac\.th|or\.th|go\.th)”
38
[email protected]
RegularExpressionValidator
39
[email protected]
RegularExpressionValidator
<%@Page language="C#"%>
<html>
<head><title>Regular Expression Validator</title></head>
<body>
<form action="example5.aspx" runat="server">
Email : <asp:textbox id="txtemail" runat="server" />
<asp:RegularExpressionValidator id="check1" ErrorMessage="Not Valid !!!"
ControlToValidate="txtemail" runat="server"
ValidationExpression
="[\w-]+@[\w-]+\. (com|net|org|co\.th|ac\.th|or\.th|go\.th)"/><br>
Tel No. : <asp:textbox id="txtphone" runat="server" />
<asp:RegularExpressionValidator id="check2" ErrorMessage="Not Valid !!! (xx) xxx-xxxx
Or (xxx) xxx-xxx" ControlToValidate="txtphone" runat="server"
ValidationExpression
="(\(0[0-9]{1}\)\s[0-9]{3}-[0-9]{4}|\(0[0-9] {2}\)\s[0-9]{3}-[0-9]{3})"/>
<br>
(ต่อ) 
40
[email protected]
RegularExpressionValidator
Zip Code : <asp:textbox id="txtzip" runat="server" />
<asp:RegularExpressionValidator id="check3" ErrorMessage="Not Valid !!!"
ControlToValidate="txtzip" runat="server"
ValidationExpression="[0-9]{5}"/><br>
<asp:button id="submit" runat="server" text="Submit" /><br>
</form>
</body>
</html>
41
[email protected]
CustomValidator
Number: <asp:TextBox id="_num" runat=server/>
<asp:CustomValidator id="numValidator" runat=server
ControlToValidate="_num” Display=“static” InitialValue=“”
ClientValidationFunction = "ValModThreeClient"
OnServerValidate = "ValModThreeServer”
ErrorMessage="Please enter a value divisible by three" />
42
[email protected]
CustomValidator
<script language="C#" runat=server>
void ValModThreeServer(object source,
ServerValidateEventArgs e)
{
e.IsValid = false;
try
{
int num = Convert.ToInt32(e.Value);
if (num % 3 == 0)
e.IsValid = true;
}
catch (Exception)
{}
}
</script>
<script language=javascript>
<!-function ValModThreeClient( source, args){
if ( value % 3 )
args.IsValid = false;
else
args.IsValid = true;
}
-->
</script>
43
[email protected]
ทส215
การเขียนโปรแกรมบนเว็บ 1
Error & Exception
ั
อาจารย์อรรถวิท ชงคมานนท์
สาขาวิชาเทคโนโลยีสารสนเทศ
คณะวิทยาศาสตร์
www.itsci.mju.ac.th
44
[email protected]
What is an Exception?
 คือเงือ
่ นไขการทางานทีผ
่ ด
ิ พลาด หรือ ไม่สามารถกาหนดพฤติกรรมของเหตุการณ์นัน
้ ๆ ได ้
 Exceptions สามารถเพิม
่ ขึน
้ ได ้ โดยการป้ องกันความผิดพลาดของการเขียนโปรแกรม
็ ได ้ เนือ
 การป้ องกันข ้อผิดพลาดในการเขียนโปรแกรมสามารถเชค
่ งจากการตรวจสอบ syntax
โดย compiler แต่ในขณะ runtime เราไม่สามารถตรวจสอบได ้ว่าจะเกิดขึน
้ ชว่ งเวลาไหน
45
[email protected]
Why Exceptions?
 ข ้อดีของ Exceptions
 สามารถตรวจสอบเงือ
่ นไขทีผ
่ ด
ิ พลาดของโปรแกรม เพือ
่ ป้ องกันความผิดพลาด
ของข ้อมูล
 ข ้อมูลทีไ่ ม่ถก
ู ต ้องจะไม่สามารถทางานในโปรแกรมต่อได ้
่ การป้ องกันผู ้ใชป้้ อนข ้อมูล
 ข ้อผิดพลาดบางอย่างเราสามารถแก ้ไขได ้ เชน
ผิดพลาด
ื่ ถือให ้กับโปรแกรม
 สร ้างความน่าเชอ
46
[email protected]
Error Type
่
 Syntax Error ความผิดพลาดทางไวยากรณ์ เชน
iff a = b {
}
่
 Runtime Error ความผิดพลาดขณะรันโปรแกรม เชน
int a = 0;
string s = “web1”;
a = s * 10;
 Logical Error ความผิดพลาดทางตรรกะ โดยไม่ได ้เขียนคาสงั่ ผิด และไม่ม ี
ข ้อผิดพลาดในขณะรันโปรแกรม แต่เกิดจากความผิดพลาดทางแนวคิดของผู ้เขียน
โปรแกรม
47
[email protected]
What is Exception Management
 เป็ นวิธจ
ี ัดการสาหรับ
 หาข ้อยกเว ้นของโปรแกรม
 การบันทึกเหตุการณ์และรายงานผลข ้อมูล
 จัดการข ้อผิดพลาดเพือ
่ ให ้สามารถทางานต่อได ้
48
[email protected]
Exception Handling
Exception
ถูกเขียนเพิม
่ ลงไปใน
method
Exception
เพิม
่ เข้าไปในสว่ นของ
การเรียกใชง้ าน
No
Exception สามารถ
ตรวจสอบได้ ?
เพิม
่ code สว่ นของ
exception
Yes
่ ระบวนการแก้ไข
เข้าสูก
สามารถแก้ไขได้ ?
No
เพิม
่ สว่ นของข้อมูล
Yes
แก้ไขจาก exception
49
[email protected]
Unhandled Exceptions
Unhandled
exception
รวบรวมข้อมูล
Data store
บ ันทึกเหตุการณ์
error
Sync or Async
่ ข้อความเตือน
สง
Sync or Async
่ ระบวนการ
เข้าสูก
แก้ไข
Notification
แสดงข้อมูลให้
ก ับผูใ้ ช ้
50
[email protected]
การใช ้ Exception Handling
ั ซอน
้ มีการใชงาน
้
 เป็ นการตรวจสอบข ้อผิดพลาดทีซ
่ บ
3 แบบหลัก ๆ ดังนี้
 การตรวจสอบข ้อผิดพลาดแบบ try…catch
 การตรวจสอบข ้อผิดพลาดแบบ try…catch…finally
 การใช ้ throw
51
[email protected]
try…catch
try{
try to do anything
}catch(ตรวจจับ Error 1){
show message if found error 1
}catch(ตรวจจับ Error 2){
show message if found error 2
}
52
[email protected]
การใช ้ Try…Catch
private void btnRun_Click(object sender, System.EventArgs e)
{
double y = 0.0;
try
{
y = double.Parse(tbxNumber.Text);
Label1.Text = "10/" + y + " = " + (10/y);
Label2.Text = "Sqrt(" + y + ") = " + Math.Sqrt(y);
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
53
[email protected]
การใช ้ Try…Catch
54
[email protected]
การใช ้ Try…Catch…Finally


้
Finally ใชในการก
าหนดคาสงั่ ทีต
่ ้องการให ้ทาเสมอ ไม่วา่ จะเกิด Exception
หรือไม่ก็ตาม
รูปแบบ
Try {
………
}
Catch (Exception ex) {
………
}
Finally {
………
}
55
[email protected]
การใช ้ Try…Catch…Finally
private void btnRun_Click(object sender, System.EventArgs e)
{
double y = 0.0;
try {
y = double.Parse(tbxNumber.Text);
Label1.Text = "10/" + y + " = " + (10/y);
Label2.Text = "Sqrt(" + y + ") = " + Math.Sqrt(y);
}
catch (Exception ex) {
Label1.Text = ex.Message;
}
finally {
Label3.Text = "Calculate finished";
}
}
56
[email protected]
การใช ้ Try…Catch…Finally
57
[email protected]
การใช ้ throw


เป็ นการให ้ระบบเป็ นฝ่ ายตรวจสอบข ้อผิดพลาดเอง
รูปแบบ
throw new Exception(“ข ้อความทีต
่ ้องการแสดง”)
58
[email protected]
การใช ้ throw
private void calculate()
{
double y = 0.0;
try
{
y = double.Parse(tbxNumber.Text);
Label1.Text = "10/" + y + " = " + (10/y);
Label2.Text = "Sqrt(" + y + ") = " + Math.Sqrt(y);
}
catch (Exception ex)
{
ex = new Exception("calculate error exception!!!");
throw ex;
}
}
59
[email protected]
การใช ้ throw
private void btnRun_Click(object sender, System.EventArgs e)
{
try
{
calculate();
}
catch (Exception ex)
{
จะเอาข้อความที่ Throw จาก method
Label1.Text = ex.Message;
calculate() มาแสดง
}
}
60
[email protected]
การใช ้ throw
61
[email protected]
Standard Exceptions

















OutOfMemoryException
NullReferenceException
InvalidCastException
ArrayTypeMismatchException
IndexOutOfRangeException
ArithmeticException
DevideByZeroException
OverFlowException
ArgumentNullException
SqlException
SecurityException
ArgumentException
PathTooLongException
DirectoryNotFoundException
UnauthorizedAccessException
FileNotFoundException
………………………………………………
62
[email protected]
Error Page
 ในการกาหนดให ้มีการเรียกใช ้ Page เมือ
่ มีข ้อผิดพลาดเกิดขึน
้ ให ้กาหนด URL ของ
้ ้ใน Property “ErrorPage” ของคาสงั่ “@Page”
Page ทีต
่ ้องการให ้ถูกเรียกใชไว
 และจะต ้องกาหนดไฟล์ “web.config” ขึน
้ ไว ้ใน Virtual Directory เดียวกับ Page ที่
สร ้างขึน
้
 โดยในไฟล์ “web.config” นี้ ให ้กาหนดค่าของ Property “mode” ในสว่ น
“<customErrors>” เป็ น “On”
63
[email protected]
Error Page
<%@ Page ErrorPage=error.htm %>
<configuration>
<system.web>
<customErrors mode=”On”>
</customErrors>
</system.web>
</configuration>
<configuration>
<system.web>
<customErrors defaultRedirect = “error.aspx” mode=”On”>
<error statusCode=”404” redirect=”FileNotFound.html”>
<error statusCode=”500” redirect =”InternalError.html”/>
</customErrors>
</system.web>
</configuration>
64
[email protected]
65
[email protected]