Grid Implementation of Asp. Net Server Control Development (2)

Source: Internet
Author: User

Grid Implementation of Asp. Net Server Control Development (2)

Let's first implement the Grid class. The Code is as follows:

Grid. cs

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. web. UI; namespace AspNetServerControl {[Designer ("AspNetServerControl. design. gridDesigner, AspNetServerControl. design ")] // supported during Design, you need to write another pair of classes [ToolboxData (" <{0 }: grid Title = \ "Grid \" runat = \ "server \">
 
 ")] [ToolboxBitmap (typeof (Grid)," toolbox.Grid.bmp ")] [Description (" Table Control ")] [ParseChildren (true)] [PersistChildren (false)] [ControlBuilder (typeof (NotAllowWhitespaceLiteralsBuilder)] public class Grid: ControlBase {# region attribute # region DataSource private object _ dataSource; public object DataSource {get {return _ dataSource ;} set {_ dataSource = value ;}# endregion # region Columns private GridColumnCollection _ columns ;////// Column data ///[Category (CategoryName. OPTIONS)] [policyparentproperty (true)] [PersistenceMode (PersistenceMode. innerProperty)] [Editor (typeof (GridColumnsEditor), typeof (System. drawing. design. UITypeEditor)] // used in the editor public virtual GridColumnCollection Columns {get {if (_ columns = null) {_ columns = new GridColumnCollection (this);} return _ columns ;}} # endregion protected override void Render (HtmlTextWriter writer) {base. render (writer); if (_ columns = null) {return;} writer. write ("
 
 
  
  
"); RenderHeader (writer); RenderBody (writer); writer. Write ("
 
 
");} Private void RenderHeader (HtmlTextWriter writer) {writer. Write (""); Foreach (GridColumn column in Columns) {writer. Write (String. Format ("{0}", Column. HeaderText);} writer. Write ("");} Private void RenderBody (HtmlTextWriter writer) {DataTable dt = DataSource as DataTable; if (dt = null | dt. rows. count <= 0) {return;} writer. write (""); Foreach (DataRow row in dt. Rows) {writer. Write (""); Foreach (GridColumn column in Columns) {writer. Write (String. Format ("{0}", Row [column. DataField]);} writer. Write ("");} Writer. Write ("");}}}
1. the following attributes of the Grid class are very important:

(1). Designer is supported during design. You need to write another class library to support it.

(2 ). parseChildren indicates how the page analyzer processes the nested content in the server control tag declared on the page, that is, this attribute determines that other content can be nested inside the Grid (true), such as Columns.

2. inherit from ControlBase, which will be introduced later.

3. The DataSource attribute is the data source.

4. columns is the internal column of the Grid, where [Editor (typeof (GridColumnsEditor), typeof (System. drawing. design. UITypeEditor)] This feature is very important and indicates that Columns can be edited. Here, GridColumnsEditor is the class for interface editing, which will be introduced later.

5. Reload the Render method. This method outputs the final html format of the Grid. Grid is implemented as a table.

(1) present the column names of all Columns in th in table

(2) convert the data source to a able, traverse each row, and present the data of each row in the form of td.

The following describes the ControlBase class.

ControlBase. cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web.UI;namespace AspNetServerControl{    public class ControlBase : Control, INamingContainer    {    }}
The ControlBase class inherits from the Control class and the INamingContainer interface, which is required by custom controls.

Let's take a look at the NotAllowWhitespaceLiteralsBuilder class.

Using System; using System. Collections. Generic; using System. Linq; using System. Text; using System. Threading. Tasks; using System. Web. UI; namespace AspNetServerControl {////// The control cannot contain strings in the non-label format ///Internal class NotAllowWhitespaceLiteralsBuilder: ControlBuilder {////// Blank characters are not allowed //////
 Public override bool AllowWhitespaceLiterals () {return false ;}////// Ignore strings outside the tag //////Public override void AppendLiteralString (string s) {} public override Type GetChildControlType (string tagName, System. Collections. IDictionary attribs) {return base. GetChildControlType (tagName, attribs );}}}
NotAllowWhitespaceLiteralsBuilder is mainly used to control the non-label strings in the control.

The following describes the definitions of the GridColumnsEditor and GridColumn fields. For more information, seeGrid Implementation of Asp. Net Server Control Development (II)



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.