The previous article describes specific ways to implement 3 forms of complex properties. To deepen the reader's understanding of these implementation methods, this article explains in detail an example of creating complex attributes using ASP.net 2.0 technology.
1. Sample Application
The example that is implemented in this article is simple, and its core is the implementation of complex properties, using the content described in the previous article, by implementing complex properties of the hyphen form of a custom server control. The example effect figure is shown in Figure 1.
Figure 1
As shown in Figure 1, the page shows the company's city, name, gender, and job information. These are the results that define the rendering of the server control, where the company is located in the Simple property city setting, and the name, gender, and title are set by the complex property employee, which includes the child attribute Name,sex and title settings. The Default.aspx file source code for the sample application is listed below.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Namespace="WebControlLibrary" Assembly="WebControlLibrary" TagPrefix="Cp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<title>实现连字符形式复杂属性</title>
<body>
<form id="form1" runat="server">
<div>
<Cp:Company ID="demo1" runat="server" City="重庆" Employee-Name=" 小李" Employee-Sex="男" Employee-Title="销售经理" />
</div>
</form>
</body>
As shown in the code above, the @ Register directive and the custom server control company are mainly set. The former is used to introduce a custom server control company to the page, thereby implementing the control's application in the page. The city, Employee-name, Employee-sex, and Employee-title are mainly set up in the custom server control company. At the same time, in the developer coding process, all 4 of these properties are supported by Visual Studio 2005 's IntelliSense capabilities.
In addition, the reader can also set the company control properties in a different, non hyphen character form. The specific code looks like this:
<Cp:Company ID="Company1" runat="server" City="重庆">
<Employee Name="小李" Sex="男" Title="销售经理">
</Employee>
</Cp:Company>
In fact, the above method of setting the company control property is exactly the same as the previous method of using hyphens to set properties. You can use either of these hyphens for all hyphen-form properties. If it is based on the readability of the code, the latter is more readable than the former.