Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
142
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

Creating a Web User Control

Using the Web User Control

Once the user control has been created, it can be referenced from any ASP.NET page using the Register directive, as follows:

<%@ Register TagPrefix="prefix" TagName="name" Src="source.ascx" %>

The Register directive requires three attributes:

TagPrefix

the prefix for the user control, which allows you to group related controls together, and avoid naming conflicts

TagName

the control’s tag name, which will be used when the control is added to the ASP.NET page

Src

the path to the .ascx file that describes the user control

After registering the control, we create instances of it using the <TagPrefix:TagName> format. Let’s try an example that uses the SmartBox control. Create a new file named ControlTest.aspx in your Learning folder, and give it this content:

File: ControlTest.aspx (excerpt)

<%@ Register TagPrefix="sp" TagName="SmartBox" Src="SmartBox.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>Creating ASP.NET Web Server Controls</title> </head>

<body>

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

<sp:SmartBox id="nameSb" runat="server" LabelText="Name:" /> <sp:SmartBox id="addressSb" runat="server"

LabelText="Address:" />

<sp:SmartBox id="countrySb" runat="server" LabelText="Country:" />

<sp:SmartBox id="phoneSb" runat="server" LabelText="Phone:" />

</form>

131