3. XÂY DỰNG ASP
SERVER USER CONTROL
- User control là tập hợp các control, do người lập trình
tạo ra nhằm tăng tốc độ lập trình.
-
Được viết trong các file .ascx
-
Để tạo ra một User Control, thực hiện các bước sau:
+
Vào menu Project\Add New Item…
+
Tại danh sách “Templates”, chọn “Web User Control”
+
Nhập tên file vào ô “Name”
Ví dụ tạo Login Control
Ví dụ tạo Login
Control
Thiết kế giao diện trong file Login.ascx
<%@ Control Language="C#" %>
<table
border="1" style="background-color:#CEEDFA" cellpadding="5%" > <tr>
<td><p>
<asp:Label ID="lblUserName"
Width="45%"
Text="User Name" runat="server"/>
<asp:TextBox ID="txtUserName" Width="50%" runat="server"
/>
<br /><br />
<asp:Label ID="lblPassword"
Width="45%"
Text="Password" runat="server"/>
<input
type="password" ID="txtPassword"
style="width:50%" runat="server" />
</p></td>
</tr> </table>
Tạo các thuộc tính cho control
<script
language="C#" runat="server">
public string CaptionUserName
{
set { this.lblUserName.Text
= value; }
}
public string CaptionPassword
{
set { this.lblPassword.Text
= value; }
}
public string UserName
{
get
{ return this.txtUserName.Text; } set { this.txtUserName.Text
= value; }
}
public string Password
{
get { return this.txtPassword.Value;
}
}
</script>
Sử dụng Login Control trong Web Form (Control cần được
đăng ký bằng tag
<%@
Register %>)
<%@ Page Language="C#" %>
<%@ Register TagPrefix="vst" TagName="LoginControl" Src="Login.ascx" %>
<html>
<head
runat="server"> <title>Untitled
Page</title> </head>
<body>
<form id="form1"
runat="server">
Login Control với các thuộc tính mặc định
<vst:Login Control runat="server" />
<br />Login Control với các thuộc tính tùy chọn
<vst:LoginControl runat="server"
CaptionPassword="Mật
khẩu" CaptionUserName="Người dùng"
UserName="vusongtung" Password="idontknow"
/>
</form> </body> </html>
(Hết)
---------------------------------------------------
---------------------------------------------------
Cùng bài học
Không có nhận xét nào:
Đăng nhận xét