Grid implementation of ASP. NET Server Control development (II.)

Source: Internet
Author: User
Tags html form tagname

Let's first implement the Grid class with the following code:

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")]//design-time support, you need to write an additional pair of classes [    ToolBoxData ("<{0}:grid title=\" grid\ "runat=\" Server\ "><Columns></Columns></{0}:Grid>")] [ToolboxBitmap (typeof (Grid), "toolbox. Grid.bmp ")] [Description (" Table control ")] [ParseChildren (true)] [PersistChildren (false)] [ControlBuilder (typeof (Notall        Owwhitespaceliteralsbuilder)] public class Grid:controlbase {#region Property #region DataSource        Private Object _datasource;            Public object DataSource {get {return _datasource;}        set {_datasource = value;}        } #endregion #region Columns private gridcolumncollection _columns; <summary>///Column data//</summary> [Category (Categoryname.options)] [Notifyparentproperty (True ] [PersistenceMode (Persistencemode.innerproperty)] [Editor (typeof (Gridcolumnseditor), typeof (System.Drawing .                Design.uitypeeditor))]//for editor public virtual Gridcolumncollection Columns {get {                if (_columns = = null) {_columns = new gridcolumncollection (this);            } return _columns;            }} #endregion #endregion protected override void Render (HtmlTextWriter writer) { Base.            Render (writer);            if (_columns = = null) {return; } writer.            Write ("<table border=\" 1\ "cellspacing=\" 0\ ">");            Renderheader (writer);            Renderbody (writer); Writer.        Write ("</table>"); } private void Renderheader (HTMLTextWriter writer) {writer.            Write ("<tr>"); foreach (gridcolumn column in Columns) {writer. Write (String.Format ("<th >{0}</th>", column.            HeaderText)); } writer.        Write ("</tr>");            } private void Renderbody (HtmlTextWriter writer) {DataTable dt = DataSource as DataTable; if (dt = = NULL | | dt.            Rows.Count <= 0) {return; } writer.            Write ("<tbody>"); foreach (DataRow row in dt. Rows) {writer.                Write ("<tr >"); foreach (gridcolumn column in Columns) {writer. Write (String.Format ("<td >{0}</td>", Row[column.                DataField])); } writer.            Write ("</tr>"); } writer.        Write ("</tbody>"); }    }}
Several properties in front of the 1.Grid class are quite important:

(1). Designer is design-time support and requires a separate class library to support.

(2). ParseChildren indicates how the page parser should handle nested content in a server control tag declared on a page, that is, this property determines that other content can be nested inside the grid (nested only if true), such as Columns.

2. Inherit from Controlbase, which is described later in this class.

The 3.DataSource property is the data source.

4.Columns is the inner column of the grid, where [Editor (typeof (Gridcolumnseditor), typeof (System.Drawing.Design.UITypeEditor))] is a very important feature, This attribute indicates that the attribute columns can be edited in edit. Where Gridcolumnseditor is the interface editing class, which is described later.

5. Reload the Render method. This method outputs the final HTML form of the grid. The grid is implemented here as a table.

(1) Renders the column names of all columns in the columns as th in table

(2) Convert the data source into a DataTable, then iterate through each row and present the data in each row as TD.

Here's a look at 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 for custom controls.

And 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{///<summary>///non-tagged strings are not allowed within controls///</summary&gt    ;        Internal class Notallowwhitespaceliteralsbuilder:controlbuilder {//<summary>///white space characters not allowed        </summary>//<returns></returns> public override bool Allowwhitespaceliterals ()        {return false; }///<summary>//ignore strings that are outside the label///</summary>//<param name= "s" ></par am> public override void Appendliteralstring (string s) {} public override Type Getchildcon Troltype (String tagName, System.Collections.IDictionary attribs) {return base.        Getchildcontroltype (TagName, attribs); }    }}
Notallowwhitespaceliteralsbuilder is primarily used to control strings that are not allowed to be in non-tagged form inside the control.

The definitions for the Gridcolumnseditor and GridColumn fields are described later in this report, see the grid implementation of the ASP. NET Server Control development (ii)



Grid implementation of ASP. NET Server Control development (II.)

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.