2. CÁC ASP.NET WEB
CONTROL
-
Giới thiệu;
-
Các Web Control cơ bản;
-
Các Control kiểm soát dữ liệu;
2.1. GIỚI THIỆU
Các ASP.NET Web Control:
-
Được định nghĩa trong System.Web.UI.WebControls namespace.
-
Có thể chia là 3 nhóm chính:
+
Các Web Control cơ bản;
+
Các Control kiểm soát dữ liệu;
Sử dụng các Label, TextBox, RadioButton, CheckBox và
DropDownList.
Tạo giao diện
<%@ Page
Language="C#" Debug="true" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="ViDu222._Default"
%>
<html>
<head runat="server"> <title>Untitled
Page</title>
</head>
<body>
<form id="form1"
runat="server">
<h3>Bảng tính lương</h3>
<asp:Label Text="Tháng:"
Width="120px" runat="server" />
<asp:DropDownList ID="ddlThang"
Width="100px" runat="server" /><br /><br />
<asp:Label Text="Hệ số lương:"
Width="120px" runat="server" />
<asp:TextBox ID="txtHeSoLuong"
Width="100px" runat="server" /><br />
<h4>Các chi phí bảo hiểm:</h4>
<asp:CheckBox ID="chkBHXH" Text=" Bảo hiểm xã
hội" Checked="true" runat="server" />
<h4>Loại tiền tệ:</h4>
<asp:RadioButton ID="rdbVND" Text=" VNĐ"
GroupName="groupTienTe" runat="server"
Checked="true" />
<asp:RadioButton ID="rdbUSD" Text=" USD"
GroupName="groupTienTe" runat="server" /><br
/><br />
1 USD =
<asp:TextBox
ID="txtTyGia" Text="20000" Width="100px"
runat="server" /> VNĐ<br /><br />
<asp:Button ID="bnTinh"
Text="Tính" Width="120px" runat="server"
/><br /><br
/>
<asp:Label ID="lblTitle"
Font-Size="Medium" Font-Bold="true"
runat="server" /><br /><br />
<asp:Label
ID="lblLuongTheoHeSo" runat="server" /><br />
<asp:Label ID="lblBHXH" runat="server" /><br />
<asp:Label ID="lblBHYT" runat="server" /><br
/><br />
<asp:Label ID="lblThucNhan"
Font-Size="Medium" Font-Bold="true"
ForeColor="Red" runat="server" />
</form>
</body>
Code-Behind xử lý các sự kiện
namespace ViDu222
{
public partial class _Default :
System.Web.UI.Page
{
protected void Page_Load(object sender,
EventArgs e)
{
if (!this.IsPostBack)
{
for (int i = 0;
i < 12; i++) this.ddlThang.Items.Add(string.Format("{0}", i + 1));
this.ddlThang.SelectedIndex =
DateTime.Today.Month - 1;
}
this.bnTinh.Click += new
EventHandler(bnTinh_Click);
}
void bnTinh_Click(object sender, EventArgs e)
{
double hsl
= double.Parse(this.txtHeSoLuong.Text); double luong = hsl * 840000;
if (this.rdbUSD.Checked)
int bhxh =
this.chkBHXH.Checked ? 5 : 0; int bhyt = this.chkBHYT.Checked ? 1 : 0;
this.lblTitle.Text = string.Format("Bảng lương tháng
{0}", this.ddlThang.SelectedIndex + 1);
this.lblLuongTheoHeSo.Text =
string.Format("Lương theo hệ số: {0}", luong);
this.lblBHXH.Text =
string.Format("Trừ bảo hiểm xã hội: {0}", bhxh * luong
/ 100); this.lblBHYT.Text =
string.Format("Trừ bảo hiểm y tế: {0}", bhyt * luong /
100); this.lblThucNhan.Text = string.Format(
"Thực
nhận: {0} {1}", (100 - bhxh - bhyt) * luong / 100, this.rdbUSD.Checked?
"USD" : "VNĐ");
}
}
Sử dụng class trừu
tượng ListControl:
- Là class cơ sở của
các control mang tính danh sách như CheckBoxList, DropDownList,
ListBox và RadioButtonList
-
Các thuộc tính chính:
+ Items –
danh sách các item
+ SelectedItem – item đang chọn
+ SelectedIndex – chỉ số của item đang chọn
Ví
dụ:
Tạo giao diện:
<form id="form1"
runat="server">
<asp:CheckBoxList ID="checkBoxList1"
BorderWidth="1" AutoPostBack="true"
runat="server"
OnSelectedIndexChanged="checkBoxList1_SelectedIndexChanged">
<asp:ListItem
Text="Hoa hồng" value="15000" /> <asp:ListItem
Text="Hoa tulip" value="12000" /> <asp:ListItem
Text="Hoa thủy tiên" Value="25000" />
</asp:CheckBoxList>
<br />
<asp:Label
ID="label1" runat="server" /> </form>
Đoạn thiết lập các
item có thể được viết bằng code C# trong sự kiện Page_Load:
protected void Page_Load(object sender,
EventArgs e)
{
if (!this.IsPostBack)
{
this.checkBoxList1.Items.Add(new
ListItem("Hoa hồng", "15000"));
this.checkBoxList1.Items.Add(new ListItem("Hoa tulip",
"12000")); this.checkBoxList1.Items.Add(new ListItem("Hoa thủy
tiên", "25000"));
}
}
Trong trường hợp trên ListControl không chứa các value,
có thể dùng mảng hoặc danh sách (như ArrayList) các string để thiết lập các
item:
protected void Page_Load(object sender,
EventArgs e)
{
if (!this.IsPostBack)
{
string[]
items = new string[]{"Hoa hồng", "Hoa tulip", "Hoa thủy
tiên"}; this.checkBoxList1.Items.DataSource = items;
this.checkBoxList1.DataBind();
}
}
Xử lý sự kiện:
<script language="C#"
runat="server">
void checkBoxList1_SelectedIndexChanged(object
sender, EventArgs
e)
{
int tong = 0;
foreach (ListItem item in this.checkBoxList1.Items) if
(item.Selected)
tong += int.Parse(item.Value);
this.label1.Text = "Phải trả " +
tong.ToString() + " đồng";
}
</script>
(Còn tiếp...)
---------------------------------------------------------
---------------------------------------------------------
Cùng bài học
Không có nhận xét nào:
Đăng nhận xét