asp.net2.0 server control creation complex properties

Source: Internet
Author: User
Tags constructor implement object model tostring visual studio
Asp.net| Create | server | controls Previous ArticleThe concrete methods of implementing 3 kinds of complex attributes are introduced. 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> implement a hyphen form complex property </title>
<body>
<form id= "Form1" runat= "Server"
<div>
<cp:company id= "Demo1" runat= "Server" city= "Chongqing" employee-name= "Xiao Li" employee-sex= "male" employee-title= "Sales Manager"/>
</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= "Chongqing"
<employee name= "Xiao Li" sex= "male" title= "Sales Manager"
</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.

   2. Implementation methods

The Default.aspx page in the previous section contains a company control that has 3 characters of complex properties. How are they implemented? In fact, the key to implementing this form of complex property is to set specific design-time metadata for complex properties and their child properties during the custom server control implementation.

For complex properties, you set two design-time metadata primarily before this property is implemented: DesignerSerializationVisibility and Notifyparentproperty. DesignerSerializationVisibility is used to specify the type of persistence used when serializing a property on a component at design time. Notifyparentproperty allows modification notification of a child property in the property browser to be uploaded to the object model, and a change notification is generated in a control that has been modified. The design-time metadata setting for a child property is simpler, and you can simply set a notifyparentproperty before the child property is implemented.

Implementing a custom server control company involves two files: Company.cs and Employee.cs. The former is the implementation body of a custom server control, which includes various property settings, control rendering methods RenderContents, and so on. The latter is used to implement complex attribute employee. The following is the first listing of the Company.cs file source code.

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Text;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Namespace webcontrollibrary{
[Defaultproperty ("Text")]
[ToolBoxData ("<{0}:company runat=server> </{0}:Company>")]
public class Company:webcontrol {
Private employee employee; Implementing Properties City
[Bindable (True), Category ("appearance"), DefaultValue (""), Description ("Company City")]
public string City {
get {
String _city = (string) viewstate["City"];
Return ((_city = null)? string.empty:_city);
}
set {viewstate["city"] = value;}
}//Implementation attribute employee

[Bindable (True), Category ("appearance"), Description ("Employee Information"), DesignerSerializationVisibility ( designerserializationvisibility.content), Notifyparentproperty (true)]

Public employee Employee {
get {
if (employee = = null) {
Employee = new Employee ();
}
return employee;
}
}//Rewrite RenderContents method, custom implementation control rendering

protected override void RenderContents (HtmlTextWriter output) {
Output. Write ("Company City:");
Output. Write (city);
Output. Writebreak ();
Output. Write ("Name:");
Output. Write (Employee.Name.ToString ());
Output. Writebreak ();
Output. Write ("Sex:");
Output. Write (Employee.Sex.ToString ());
Output. Writebreak ();
Output. Write ("title:");
Output. Write (Employee.Title.ToString ());
}
}
}
The above code shows the implementation of the custom server control company, which mainly includes the contents of some properties and RenderContents methods. The specific properties include 2: One is the simple property city and the other is the complex attribute employee. The implementation of the simple property city uses the view state viewstate. Complex attribute employee is somewhat special, and its type is a class employee. At the same time, the property also has two metadata properties set: DesignerSerializationVisibility (designerserializationvisibility.content) and Notifyparentproperty (true). The former can be used to specify that the serializer should serialize the properties of the property, which is the child attribute, which enables the notification of modification of the child property in the property browser to be uploaded to the object model and produces a change notification in the control that is modified. The setting of the above two design-time metadata attributes is one of the keys to implementing complex properties in the form of hyphens. Another key point is that the metadata properties are not set when implementing the child properties of a complex property.

The following lists the Employee.cs file source code that implements the complex attribute employee.

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Globalization;
Using System.Web.UI;
Namespace webcontrollibrary{
public class Employee {
private string _name;
private string _sex;
private string _title; Implement constructor 1
Public Employee () {}//Implementation constructor 2
Public Employee (String Name, String Sex, String Title) {
_name = name; _sex = sex; _title = title;
}//Implementation property name
[Bindable (True), Category ("appearance"), DefaultValue (""), Description ("Employee Name"), Notifyparentproperty (true)]

Public String Name {
get {return _name;}
set {_name = value;}
}//Implementation properties sex

[Bindable (True), Category ("appearance"), DefaultValue (""), Description ("Employee Sex"), Notifyparentproperty (true)]

Public String Sex {
get {return _sex;}
set {_sex = value;}
}//Implementation property title

[Bindable (True), Category ("appearance"), DefaultValue (""), Description ("employee title"), Notifyparentproperty (true)]

Public String Title {
get {return _title;}
set {_title = value;}
}
}
}
The above code implements the Employee class, which includes constructors and property name, sex, and title. Readers need to be aware that the above 3 properties must set the Metadata property Notifyparentproperty (True) in order to implement a hyphen-form complex property. This way, when a child property changes. NET Framework will automatically generate a change notification and notify the parent attribute employee.

   3. Summary

This article introduces an implementation method for creating complex attributes in a hyphen form through a typical example. For other forms of complex properties, such as internal nested form complex properties, internal nested form default complex attributes, and so on, this article will not do more explaining. In fact, the implementation of complex attributes has its own laws to follow. As long as the reader is able to do so in accordance with the prescribed method, there will not be much problem.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.