1. Trang chủ
  2. » Công Nghệ Thông Tin

6 lap trinh ASP NET dieu khien do nguoi dung dinh nghia

19 438 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Chương 6: Điều khiển do người dùng định nghĩa
Thể loại Giáo trình
Năm xuất bản 2013
Định dạng
Số trang 19
Dung lượng 841,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

6 lap trinh ASP NET dieu khien do nguoi dung dinh nghia

Trang 1

DO NGƯỜI DÙNG ĐỊNH NGHĨA

Trang 2

Nội dung

 Giới thiệu

 Tạo User Control

Trang 3

Giới thiệu Web User Control

 Ngoài những điều khiển nội tại (contrict control), MS Visual Studio NET cung cấp khả năng tự xây

dựng các điều khiển tùy biến - điều khiển do người dùng tự xây dựng (Web User Controls)

 User Control (UC) chính là một "trang con", trong đó

có thể chứa bất kỳ nội dung nào (trừ các thẻ

<HTML> <BODY>,<FORM>, vì một trang chỉ có duy nhất một lần xuất hiện các thẻ này)

Trang 4

- Một UC có phần mở rộng là *.ascx

- UserControl không thể gọi trực tiếp từ trình duyệt,

nó phải được nhúng vào các trang aspx.

- Nội dung trang User Control được khai báo với thẻ

<%@ Control …%> như sau :

Trang 5

• Các lớp User Control kế thừa từ

System.Web.UI.UserControl

• sự thừa kế của hai đối tượng Page (trang aspx)

và User Control (trang ascx

Trang 6

Tạo User Control

13 B1: R-Click tại ứng

dụng web trong

cửa sổ Solution

Explorer và chọn

Add New Item ,

xuất hiện hộp thoại

Chọn Web User

Control, nhập tên

User Control vào

khung Name, click

nút Add

Trang 7

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Footer.ascx.cs" Inherits="Footer" %>

<asp:Label id="lblFooter" runat="server" />

Trang 8

public partial class Footer : System.Web.UI.UserControl

{

protected void Page_Load(Object sender, EventArgs e) {

lblFooter.Text = "This page was served at ";

lblFooter.Text += DateTime.Now.ToString();

} }

Trang 9

Sử dụng user control (trong trang aspx)

<%@ Page Language="C#" AutoEventWireup="true"

CodeFile="FooterHost.aspx.cs" Inherits="FooterHost"%>

<%@ Register TagPrefix="apress" TagName="Footer"

Src="Footer.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"

"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Footer Host</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<h1>A Page With a Footer</h1><hr />

Static Page Text<br /><br />

<apress:Footer id="Footer1" runat="server" />

</div></form></body></html>

Trang 10

Sử dụng user control

Trang 11

Thay vì khai báo trên file aspx, User control có thể được khai báo trong web.config như sau:

<configuration>

<system.web>

<pages>

<controls>

<add tagPrefix="apress" src="~Footer.ascx"

tagName="Footer" />

</controls>

</pages>

</system.web>

</configuration>

Có thể dùng nhiều thẻ <add> cho nhiều User control

Khai báo User control trên web.config

Trang 12

Các loại User Control

- User Control độc lập ( Independent User Control ) là loại điều khiển không tương tác với phần còn lại của các mã lệnh trên trang web Footer.ascx là một ví dụ Independent User Control.

- User Control tích hợp ( Integrated User Control ) là loại điều khiển có sự tương tác với phần còn lại của các mã lệnh trên trang web.

Trang 13

Vd Indepent User control (tạo Usercontrol)

<%@ Control Language="C#" AutoEventWireup="true"

CodeFile="LinkMenu.ascx.cs" Inherits="LinkMenu" %>

<div>

Products:<br />

<asp:HyperLink id="lnkBooks" runat="server"

NavigateUrl="MenuHost.aspx?product=Books">Books

</asp:HyperLink><br />

<asp:HyperLink id="lnkToys" runat="server"

NavigateUrl="MenuHost.aspx?product=Toys">Toys

</asp:HyperLink><br />

<asp:HyperLink id="lnkSports" runat="server"

NavigateUrl="MenuHost.aspx?product=Sports">Sports

</asp:HyperLink><br />

<asp:HyperLink id="lnkFurniture" runat="server"

NavigateUrl="MenuHost.aspx?product=Furniture">Furniture

</asp:HyperLink>

Trang 14

Vd Indepent User control (dùng Usercontrol)

<%@ Page Language="C#" AutoEventWireup="true"

CodeFile="MenuHost.aspx.cs" Inherits="MenuHost"%>

<%@ Register TagPrefix="apress" TagName="LinkMenu"

Src="LinkMenu.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"

"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server"><title>Menu Host</title></head>

<body>

<form id="form1" runat="server">

<div><table>

<tr>

<td><apress:LinkMenu id="Menu1" runat="server" /></td> <td><asp:Label id="lblSelection" runat="server" /></td>

</tr>

</table></div></form></body></html>

Trang 15

protected void Page_Load(Object sender, EventArgs e) {

if (Request.Params["product"] != null){

lblSelection.Text = "You chose: ";

lblSelection.Text += Request.Params["product"]; }

}

Vd Indepent User control (code trang aspx.cs)

Trang 16

L

Trang 17

public partial class Footer : System.Web.UI.UserControl{

public enum FooterFormat { LongDate, ShortTime }

private FooterFormat format = FooterFormat.LongDate; public FooterFormat Format{

get { return format; }

set { format = value; }

}

protected void Page_Load(Object sender, EventArgs e){ lblFooter.Text = "This page was served at ";

if (format == FooterFormat.LongDate){

lblFooter.Text += DateTime.Now.ToLongDateString(); }

else if (format == FooterFormat.ShortTime){

lblFooter.Text += DateTime.Now.ToShortTimeString(); }

}

Vd Integrated User control

Trang 18

L

Trang 19

public partial class FooterHost : System.Web.UI.Page{

protected void Page_Load(Object sender, EventArgs e){

if (optLong.Checked){

Footer1.Format = Footer.FooterFormat.LongDate;

}

else if (optShort.Checked)

{

Footer1.Format = Footer.FooterFormat.ShortTime;

}

else

{

// The default value in the Footer class will apply

}

}

}

Khởi tạo giá trị ban đầu:

/>

Ngày đăng: 06/12/2013, 12:19

TỪ KHÓA LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w