Creating User Controls
In addition to the built-in server controls in the .NET Framework, you can
also build your own controls to be placed on Web Forms. ASP.NET supports
two different types of user-created controls: user controls and custom
controls. The creation of custom controls is beyond the scope of this book,
but it's simple to create user controls. These controls are basically saved
pages (or pagelets) that act as self-contained controls.
For example, you may wish to create a menu system along the left side of
every page in your site, where each menu item is built from a database. You
might also want to place a standard logo and header on each page. What you
need is some way to include standard pages, or parts of pages, within each
of your pages.
User controls provide this functionality and are the replacement for
#include files in ASP.NET. Figure 6.4 displays the output you might want to
generate. In this example, the image and header "Northwind Traders" are
both in one user control. The menu going down the left side is another
control.
Figure 6.4. User controls output HTML on the client side.
Figure 6.5 shows the same Web page in design mode. As you can see, these
user controls are simply placeholders. You can see the full UI for such
controls by opening the file where you built them within your project.
Figure 6.5. User controls do not display any HTML on the server side; they
are just placeholders.
In addition to the built-in server controls in the .NET Framework, you can
also build your own controls to be placed on Web Forms. ASP.NET supports
two different types of user-created controls: user controls and custom
controls. The creation of custom controls is beyond the scope of this book,
but it's simple to create user controls. These controls are basically saved
pages (or pagelets) that act as self-contained controls.
For example, you may wish to create a menu system along the left side of
every page in your site, where each menu item is built from a database. You
might also want to place a standard logo and header on each page. What you
need is some way to include standard pages, or parts of pages, within each
of your pages.
User controls provide this functionality and are the replacement for
#include files in ASP.NET. Figure 6.4 displays the output you might want to
generate. In this example, the image and header "Northwind Traders" are
both in one user control. The menu going down the left side is another
control.
Figure 6.4. User controls output HTML on the client side.
Figure 6.5 shows the same Web page in design mode. As you can see, these
user controls are simply placeholders. You can see the full UI for such
controls by opening the file where you built them within your project.
Figure 6.5. User controls do not display any HTML on the server side; they
are just placeholders.
本文介绍了在ASP.NET中创建用户控件的相关内容。除了.NET框架的内置服务器控件,还可创建用户控件和自定义控件,这里着重讲用户控件。它能实现包含标准页面或部分页面的功能,可替代ASP.NET中的#include文件,如创建菜单系统、放置标准logo和标题等。
886

被折叠的 条评论
为什么被折叠?



