My ASP knowledge base

Designing the ‘inner’ elements

<< Previous page Next Page >>

When adding the custom grid to a web page later on, it should have the following lay-out:

<wit:WITGrid ID=”gridExample” runat=”server”>
    <wit:BoundField HeaderText=”id” />
    <wit:CheckBoxField HeaderText=”locked” .. />
</
wit:WITGrid>

 

The BoundField and CheckBoxField are defining the grid’s columns. This section describes the definition of these ‘inner’ elements. By the way, they are not templates! A template can only be defined once within a control. BoundField and CheckBoxField an be defined multiple times to define multiple columns.

GridField.cs
First, the class GridField is designed. This class is the basis for the inner elements. It defines the common properties ID, HeaderText, CssClass used by the other classes.

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.ObjectModel;
using System.Web.UI.WebControls;

namespace WITControlsLibrary
{
    //Gridfield
    //This class is used as a basis for BoundField, CheckBoxField, etc.
    public class GridField
    {
        #region ” Properties “

        private string _id;
        [Category("Data")]
        [Description("The header text")]
        public string ID
        {   get { return _id; } set { _id = value; }  }

        private string _headerText;
        [Category("Data")]
        [Description("The header text")]
        public string HeaderText
        {
            get { return _headerText; }
            set { _headerText = value; }
        }

        private string _cssClass;
        [Category("Appearance")]
        [Description("The column or field style")]
        public string CssClass
        {
            get { return _cssClass; }
            set { _cssClass = value; }
        }
        #endregion

        #region Constructors “
        public GridField() { }
        public GridField(string id, string headerText)
        {
            _id = id;
            _headerText = headerText;
        }
        #endregion
    }
}

 

The Category indicates under which the property appears in the Properties window.

The Description is the description in the Properties window.

the opening and closing tags of the server control.

BoundField.cs

Secondly, the class BoundField is created. This class is derived from GridField and defines a grid column which can be bound to a data field (from the grid’s datasource).

Besides the extra property DataField the class also has a IParserAccessor interface. This interface and its related function AddParsedSubObject make it possibleto recognize and parse the elements between the opening and close tag:
<BoundField ID=”test”>participantName</BoundField>

In the implementation below the information between the opening and close tags is used to the define the DataField. So the above definition of the data bound field is similar to:
<BoundField ID=”test” DataField=”participantName” />  

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.ObjectModel;
using System.Web.UI.WebControls;

namespace WITControlsLibrary
{
    public class BoundField : GridField, IParserAccessor
    {
        #region ” Properties “
       
        private string _dataField;
        [Category("Data")]
        [Description("The data field in the data source")]
        public string DataField
        {
            get { return _dataField; }
            set { _dataField = value; }
        }
        #endregion

        #region ” Constructors “
        public BoundField() { }
        public BoundField(string id, string headerText, string dataField)
        {
            ID = id;
            HeaderText = headerText;
            _dataField = dataField;
        }
        #endregion

        #region IParserAccessor Members

        public void AddParsedSubObject(object obj)
        {
            if (obj is LiteralControl)
                this.DataField  = ((LiteralControl)obj).Text;
            else if (obj is DataBoundLiteralControl)
                this.DataField = ((DataBoundLiteralControl)obj).Text;
            else
                throw new Exception(“Error parsing inner text ‘” + this.ID + “‘”);
        }
        #endregion
    }
}

 

CheckBoxField.cs

The CheckBoxField class is derived from the BoundField-class. It has the same properties, not spectacular, but by defining the CheckBoxField class the developer can show a data bound checkbox column. The CheckBoxField class is handled differently in the rendering of the grid.

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.ObjectModel;
using System.Web.UI.WebControls;

namespace WITControlsLibrary
{
    public class CheckBoxField : BoundField
    {
        #region ” Properties “

        #endregion

        #region ” Constructors “
        public CheckBoxField() { }
        public CheckBoxField(string id, string headerText, string dataField)
        {
            ID = id;
            HeaderText = headerText;
            DataField = dataField;
        }
        #endregion
    }
}

<< Previous page Next Page >>

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>