.ascx
用户控件
参考系列教程User controls in asp.net - Part 104
用户控件包含了html、代码和其他Web或者用户控件的组合,并在Web服务器上以自己的文件格式保存,其扩展名是*.ascx
用户控件重用相同的代码,从而节省了大量的开发和测试时间。简而言之,用户控制,增加了代码的可重用性,实现封装,减少开发和维护时间。
用户控件与webform的异同:
1.设计和实现用户控件与webform类似
2.webform的扩展名为.aspx
,以@Page
指令开头,可以包含<html>
, <head>
, 和<body>
元素。如:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
3.用户控件以@Control
指令开头,不能含有<html>
, <head>
, 和<body>
元素。如:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CalenderUserControl.ascx.cs" Inherits="WebApplication1.CalenderUserControl" %>
在webform中使用用户控件
参考Adding and using user controls on a webform - Part 105
在webform中使用用户控件,仅仅把用户控件拖动到webform中即可。此时webform多了如下的代码:
<%@ Register Src="~/CalenderUserControl.ascx" TagPrefix="uc1" TagName="CalenderUserControl" %>
......
<uc1:CalenderUserControl runat="server" ID<