01.
<%@ Page Language=
"C#"
AutoEventWireup=
"true"
CodeFile=
"Default.aspx.cs"
Inherits
=
"_Default"
%>
02.
04.
06.
<head runat=
"server"
>
07.
<title>Untitled Page</title>
08.
</head>
09.
<body>
10.
<form id=
"form1"
runat=
"server"
>
11.
<div>
12.
13.
เลือกรายการ :
14.
<select id=
"sltProduct"
runat=
"server"
>
15.
<option Value=
"1"
>ปาปริก้า</option>
16.
<option Value=
"2"
>โปเต้</option>
17.
<option Value=
"3"
>ฮานามิ</option>
18.
</select>
19.
จำนวน :
20.
<input type=
"text"
id=
"txtUnit"
runat=
"server"
>
21.
ราคาต่อหน่วย :
22.
<input type=
"text"
id=
"txtPricePerUnit"
runat=
"server"
>
23.
<input type=
"submit"
id=
"btnsubmit"
runat=
"server"
/>
24.
</div>
25.
<div id=
"sumDisplay"
runat=
"server"
></div>
26.
</form>
27.
</body>
28.
</html>
29.
30.
.cs
31.
using System;
32.
using System.Configuration;
33.
using System.Data;
34.
using System.Linq;
35.
using System.Web;
36.
using System.Web.Security;
37.
using System.Web.UI;
38.
using System.Web.UI.HtmlControls;
39.
using System.Web.UI.WebControls;
40.
using System.Web.UI.WebControls.WebParts;
41.
using System.Xml.Linq;
42.
43.
public partial class _Default : System.Web.UI.Page
44.
{
45.
protected void Page_Load(object sender, EventArgs e)
46.
{
47.
if (Page.IsPostBack)
48.
{
49.
sumDisplay.InnerText = DoOperation(
50.
sltProduct.Items[sltProduct.SelectedIndex].Text,
51.
txtUnit.Value,
52.
txtPricePerUnit.Value);
53.
}
54.
else
55.
{
56.
sumDisplay.InnerText =
"กรุณากรอกข้อมูล"
;
57.
}
58.
}
59.
private string DoOperation(string name, string unit, string pricePerUnit)
60.
{
61.
if (name == null || unit == null || pricePerUnit == null)
62.
{
63.
return
"ต้องกรอกข้อมูลให้ครบถ้วน"
;
64.
}
65.
int numUnit, numPricePerUnit;
66.
if (!Int32.TryParse(unit, out numUnit))
67.
{
68.
return
"ข้อมูลจำนวนไม่เป็นตัวเลข"
;
69.
}
70.
if (!Int32.TryParse(pricePerUnit, out numPricePerUnit))
71.
{
72.
return
"ข้อมูลจำนวนไม่เป็นตัวเลข"
;
73.
}
74.
return
"ราคาที่ต้องจ่ายของ"
+ name +
" = "
+ (numUnit * numPricePerUnit);
75.
}
76.
}