Click here to Skip to main content
15,867,141 members
Articles / Web Development / ASP.NET

Creating a web control to insert, update, delete, and display data

Rate me:
Please Sign up or sign in to vote.
4.35/5 (11 votes)
12 Apr 2006CPOL7 min read 68.7K   1.2K   60   6
Creating a web control to insert, update, delete, and display data.

Sample Image - DataCatalog.jpg

Introduction

One common task for web applications is to maintain data. This means to insert, update, delete, and display data. Both .NET 1.x and 2.0 have web controls that you can use to do this task. DataGrid, DataList, GridView, and DetailsView are some of the web controls that you can use to maintain data. In this article, you will see how to create a custom web control that will help you to maintain data. It is assumed that you already know how to create web controls, so we will focus our attention on the main points of our web control. You can download the code for the implementation details (Important: read the readme.txt file for installation details).

DataCatalog control overview

The DataCatalog web control allows you to display data. You can update or delete a data record by selecting it and then pressing the Update or Delete button, respectively. Click on the Insert button to add a new data record. To order the data, just click a column name, and the data will be ordered based on the selected column.

Image 2

Figure 1. DataCatalog web control

The table below has a more detailed technical explanation:

Section nameDescription
HeaderThe DataCatalog web control has a header section that contains the names of the data columns from our data source. When the user clicks a column name, the data is ordered by the selected data column.
Selection columnAt the left of each data record, a radio button is displayed to allow the user to delete or to update the selected data record.
Data record contentBelow the header section, the content of each data record is displayed. You can use the DataFieldsFormatString property to specify the format (e.g., money format) to a data field value.
Command sectionThis section contains Insert, Update, and Delete buttons. When the Insert button is clicked, the user is redirected to the insert web form. When a data record is selected and the Update button is clicked, the user is redirected to the update web form. When the Delete button is clicked, the selected data record is deleted.

DataCatalog control properties

The DataCatalog properties have three main purposes: to specify the SQL statements to select and delete data, to specify the format to be applied to the data field values, and to specify the web forms used to insert, update, and display record details.

Image 3

Figure 2. DataCatalog properties

The following table contains a description of the DataCatalog properties:

PropertyDescription
SelectTextSQL statement used to get the data to be displayed by the DataCatalog web control.
OrderByColumnSelectTextSQL statement used to order the data based on the selected data column.
DeleteTextSQL statement used to delete the selected data record.
DbConnectionTypeUsed to specify if a SQL client (SQL Server) or OLEDB data provider will be used.
DataKeyNamesData column names used to identify a data record. Specify the data field name(s) used as primary key(s).
UpdateRedirectUrlURL to which the user will be re-directed after clicking the Update button.
InsertRedirectUrlURL to which the user will be re-directed after clicking the Insert button.
DetailsRedirectUrlURL to which the user will be re-directed after selecting a data item.
DataFieldsFormatStringString containing the format to be applied to the data fields. For example, the following value is used to give format to date and money data columns, respectively: PURCHASEDATE={0:dd/MM/yyyy}, PRICE={0:c}
ColumnsStyleStringString containing the CSS styles to be applied to the columns.

DataCatalog child controls

The DataCatalog web control is a composite control. Its control hierarchy is: a Table object containing three rows. The first row corresponds to the header. The second row corresponds to the data to be displayed. The last row corresponds to the command buttons (Insert, Update, Delete).

Header

The header section is represented by the HeaderItem object. The HeaderItem object inherits from the DataCatalogItem object, which inherits from the TableRow object.

C#
public class HeaderItem : DataCatalogItem
{
    ...
}

For each data column, a TableCell object is added to the header row. Each TableCell will contain a LinkButton control that will allow the user to order the data.

C#
protected override void CreateColumnHierarchy(bool useDataSource, 
          string columnName, bool firstColumn, bool lastColumn)
{
    TableCell cell = new TableCell();
    base.Cells.Add(cell);
        
    LinkButton link = new LinkButton();
    cell.Controls.Add(link);
    
    if (useDataSource)
    {
        link.Text = columnName;
        link.CommandName = "Order";
    }

    ...
}

When the user clicks a LinkButton, the HeaderItem bubbles the event to the DataCatalog web control to order the data based on the selected data column.

C#
protected override bool OnBubbleEvent(object source, EventArgs args)
{
    CommandEventArgs args1 = args as CommandEventArgs;
    if (args1 != null)
    {
        OrderByCommandEventArgs args2 = 
                 new OrderByCommandEventArgs(source, args1);
        base.RaiseBubbleEvent(this, args2);
        return true;
    }
    return false;
}

Body

The body section contains the data. For each data record, a radio button is displayed at the left. At the right, the record data is displayed.

The radio buttons section is represented by the SelectionCell object. The SelectionCell object inherits from the TableCell object.

C#
public class SelectionCell : TableCell
{
    ...
}

For each data record, a new TableRow object is added to the selection cell. Each TableRow will contain a RadioButton control.

C#
protected override void CreateChildControls()
{
    ...
    for(int i=0; i<this._itemCount; i++)
    {
        TableRow row = new TableRow();
        table.Rows.Add(row);
        TableCell cell = new TableCell();
        row.Cells.Add(cell);
        RadioButton radio = new RadioButton();
        cell.Controls.Add(radio);                
        radio.GroupName = "ItemsGroup";    
    }
    ...
}

The record data is represented by the ContentItem object. The ContentItem object inherits from the DataCatalogItem object, which inherits from the TableRow object.

C#
public class ContentItem : DataCatalogItem
{
    ...
}

For each data column, a TableCell object is added to the content row. The first TableCell will contain a LinkButton control used to redirect the user to the DetailsRedirectUrl web form. The next TableCell objects will contain the data field values.

C#
protected override void CreateColumnHierarchy(bool useDataSource, 
          string columnName, bool firstColumn, bool lastColumn)
{
    ...
    if (firstColumn)
    {
        LinkButton link = new LinkButton();
        cell.Controls.Add(link);
        link.CommandName = "Details";

        if (useDataSource)
            link.Text = this.GetFieldValue(columnName);
    }
    else if (useDataSource)
        cell.Text = this.GetFieldValue(columnName);

    ...
}

When the user clicks the LinkButton in the first cell, the ContentItem bubbles the event to the DataCatalog web control to redirect the user to the details web form.

C#
protected override bool OnBubbleEvent(object source, EventArgs args)
{
    CommandEventArgs args1 = args as CommandEventArgs;
    if (args1 != null)
    {
        SelectItemEventArgs args2 = new SelectItemEventArgs(source, args1, this);
        base.RaiseBubbleEvent(this, args2);
        return true;
    }
    return false;
}

Footer

The footer section is represented by the CommandItem object. The CommandItem object inherits from the TableRow object.

C#
public class CommandItem : TableRow, INamingContainer
{
    ...
}

The CommandItem object contains three buttons: Insert, Update, and Delete.

C#
protected override void CreateChildControls()
{
    ...
    Button button = new Button();                
    cell.Controls.Add(button);
    button.CommandName = "Insert";
    button.Text = "Insert";
    LiteralControl space = new LiteralControl(" ");
    cell.Controls.Add(space);
    
    button = new Button();                
    cell.Controls.Add(button);
    button.CommandName = "Update";
    button.Text = "Update";
    space = new LiteralControl(" ");
    cell.Controls.Add(space);

    button = new Button();                
    cell.Controls.Add(button);
    button.CommandName = "Delete";
    button.Text = "Delete";                                
}

When the user clicks any button, the CommandItem bubbles the event to the DataCatalog web control to redirect the user to the insert or update web form, or to delete the selected data record.

C#
protected override bool OnBubbleEvent(object source, EventArgs args)
{
    CommandEventArgs args1 = args as CommandEventArgs;
    if (args1 != null)
    {
        DataOperationCommandEventArgs args2 = 
           new DataOperationCommandEventArgs(source, args1);
        base.RaiseBubbleEvent(this, args2);
        return true;
    }
    return false;
}

DataCatalog web control

The DataCatalog web control is a composite control. It is composed of the HeaderItem, SelectionCell, ContentItem, and CommandItem objects.

The main two tasks of the DataCatalog web control are: create the control hierarchy, and handle the child control events.

The CreateControlHierarchy method handles the child controls creation. The HeaderItem and CommandItem are created at the beginning and at the end, respectively. For each data item in the data source, a ContentItem object is created. A single SelectionCell object is created.

C#
protected virtual void CreateControlHierarchy(bool useDataSource)
{
    ...
    
    foreach(object dataItem in data)
    {
        if (columnNames==null)
        {

            HeaderItem headerItem = new HeaderItem(columnNames, orderByColumnName);

            if (useDataSource)
                headerItem.DataBind();

            this._dataCatalogTable.Rows.Add(headerItem);
        }

        ContentItem newItem = new ContentItem(columnNames, itemsCount, 
                              this.DataFieldsFormat, this.ColumnsStyle);

        if (useDataSource)
        {
            newItem.DataItem = dataItem;
            newItem.DataBind();
            
        }

        this._dataCatalogTable.Rows.Add(newItem);

        itemsCount++;
    }

    if (itemsCount>0)
    {
        outerRow.Cells.AddAt(0, new SelectionCell(itemsCount));
        outerTable.Rows.Add(new CommandItem()); 
    }        
    
    ...
}

The DataCatalog web control overrides the OnBubbleEvent event to handle all the events of the child controls.

C#
protected override bool OnBubbleEvent(object source, EventArgs e)
{
    if (e is DataOperationCommandEventArgs)
    {
        DataOperationCommandEventArgs args = (DataOperationCommandEventArgs)e;

        if (args.DataOperation==DataOperation.Delete)
        {                    
            DataKey dataKey = this.GetSeletedDataKey();

            if (dataKey!=null)
            {
                this.DbDataExecute.SqlDeleteText = this.DeleteText;
                if (this.DbDataExecute.ExecuteDelete(
                             this.GetDataKeyValues(dataKey), null)>0)
                    this.DataBind();
            }
        }
        else if (args.DataOperation==DataOperation.Insert)
        {
            this.Context.Response.Redirect(
              string.Format("{0}?Mode=Add", 
              this.InsertRedirectUrl), true);
        }
        else if (args.DataOperation==DataOperation.Update)
        {
            DataKey dataKey = this.GetSeletedDataKey();
        
            if (dataKey!=null)
            {
                this.Context.Response.Redirect(
                    string.Format("{0}?Mode=Edit&{1}", 
                    this.UpdateRedirectUrl, 
                    this.BuildSelectDataItemUrlString(this.GetDataKeyValues(dataKey))), 
                    true);
            }
        }

        return true;
    }
    else if (e is OrderByCommandEventArgs)
    {
        OrderByCommandEventArgs args = (OrderByCommandEventArgs)e;

        if (StringHelper.IsEmpty(this.OrderByColumnSelectText))
            throw new Exception("Specify the OrderByColumnSelectText property value.");

        this._orderByColumnName = args.ColumnName;
        this._orderByEnabled = true;

        this.DataBind();

        this._orderByColumnName = string.Empty;
        this._orderByEnabled = false;

        return true;
    }            
    else if (e is SelectItemEventArgs)
    {
        SelectItemEventArgs args = (SelectItemEventArgs)e;

        string urlString = this.BuildSelectDataItemUrlString(
            this.GetDataKeyValues(this.DataKeyManager.GetItemAt(args.Item.ItemIndex)));

        if (StringHelper.IsEmpty(this.DetailsRedirectUrl))
            throw new Exception("Specify the DetailsRedirectUrl property value.");

        this.Context.Response.Redirect(
            string.Format("{0}?Mode=ReadOnly&{1}", 
            this.DetailsRedirectUrl, urlString), true);

        return true;
    }
    return false;
}

The following table has a list of the events handled by the DataCatalog web control:

EventDescription
DeleteThe selected record is deleted. The DataKeyNames property is used to get the values that identify the record to delete.
InsertRedirects the user to the web form used to insert data.
UpdateRedirects the user to the web form used to update data. The DataKeyNames property is used to generate a query string used to identify the record to update.
OrderOrders the data by the selected data column. The OrderByColumnSelectText property is used to order the data. The [COLUMNNAME] tag is replaced with the name of the selected column.
SelectRedirects the user to the web form used to select data. The DataKeyNames property is used to generate a query string used to identify the selected record.

InsertRedirectUrl, UpdateRedirectUrl, and DetailsRedirectUrl properties

We will use the BuilderEditor web control to build the web forms used to insert, update, and display data. The BuilderEditor web control generates the controls used to insert, update, and display data. You can download a free BuilderEditor developer version here.

To create the content of the web forms used to insert, update, and display data, we need to:

  • Drag the BuilderEditor web control to the design surface.
  • Then, right-click and select the Property Builder option from the context menu option.
  • Right-click in the Available fields area, and then select the Fields editor option.
  • Image 4

  • Specify the data fields.
  • Image 5

  • Select the controls to be used to insert, update, and display data.
  • Image 6

  • Click OK to save changes.
  • The input controls to insert, update, and display data are generated.
  • Image 7

This article has covered the main features of the DataCatalog web control. For full details, download the code. Download the BuilderEditor and read the documentation for more details about how to use it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) www.sqlnetframework.com
Mexico Mexico
Luis Ramirez is creator and owner of ADO.NET Accelerator. You can use the FREE ADO.NET Accelerator version to reduce more than 50% ADO.NET code from your data access layer. Luis Ramirez is a Microsoft Certified Professional specialized in .NET development. If you want to contact him to work in your projects or for any other inquiry that you have write him to lramirez [at] sqlnetframework [dot] com.

Comments and Discussions

 
Generalthanx.. Pin
zyapiq25-Mar-07 1:56
zyapiq25-Mar-07 1:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.