Click here to Skip to main content
15,895,799 members
Articles / Web Development / XHTML

Paging, Selecting, Deleting, Editing and Sorting in the ASP.NET GridView Control with Keyboard Shortcuts

Rate me:
Please Sign up or sign in to vote.
4.89/5 (32 votes)
5 Oct 2010CPOL8 min read 158.2K   6.3K   137  
An ASP.NET 2.0 AJAX Extender to enhance the ASP.NET GridView to page, select, delete, edit and sort rows with keyboard shortcuts.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Threading;
using System.Collections.Generic;

public partial class AjaxGridControl : System.Web.UI.UserControl
{

    #region private fields
    private int gridId=0;
    AjaxGrid.GridProfileData profile;
    #endregion private fields


    #region public properties
    public int GridId { get { return gridId; } set { gridId = value; } }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        bool outputPage = true;
        List<AjaxGrid.GridProfileData> profiles = (List<AjaxGrid.GridProfileData>)Session["GridProfile"];
        profile = profiles [gridId];

        if (IsPostBack)
        {   // intercept postbacks and action them before repainting
            // (they may well jump to another page and make repaint of current table redundant)
            if (profile.SelectProc != null && Request.Form["__EVENTTARGET"] == "AJAXGRID" + gridId.ToString())
            {
                profile.SelectProc(AjaxGrid.SafeInt(Request.Form["__EVENTARGUMENT"]));
            }
            else if (Request.Form["__EVENTTARGET"] == "AJAXCSV") // postback was used to output CSV file
            {                                                    // initiate asynchronous call to output csv
                outputPage = false;
            }
        }

        // synchronous call to populate grid commented out below..
        // divGrid.InnerHtml = AjaxGrid.Paint(Session["GridProfile"], gridId);
        // initiate asynchronous call to populate grid or CSV
        AjaxGrid.StateObject stateObject = new AjaxGrid.StateObject(profile, gridId, outputPage);
        EndEventHandler endproc = (outputPage) ? new EndEventHandler(EndProc) : new EndEventHandler(EndProcCSV);
        PageAsyncTask task = new PageAsyncTask(
                    new BeginEventHandler(stateObject.BeginProc),
                    endproc,
                    new EndEventHandler(TimeoutProc),
                    stateObject
                );
        Page.RegisterAsyncTask(task);
    }

    #region aynchronous I/O completion
    // asynchronous database read failed...
    void TimeoutProc(IAsyncResult result) // default timeout is 45 sec
    {
        AjaxGrid.StateObject stateObject = (AjaxGrid.StateObject)result.AsyncState;
        stateObject.Dispose();
    }

    // asynchronous database read has completed, we can now populate grid with the results
    public void EndProc(IAsyncResult result)
    {
        AjaxGrid.StateObject stateObject = (AjaxGrid.StateObject)result.AsyncState;
        try
        {
            divGrid.InnerHtml = AjaxGrid.Paint(stateObject.EndProc(result), profile, gridId);
        }
        finally
        {
            stateObject.Dispose();
        }
    }

    // asynchronous database read has completed, we can now generate a CSV from the results
    public void EndProcCSV(IAsyncResult result)
    {
        AjaxGrid.StateObject stateObject = (AjaxGrid.StateObject)result.AsyncState;
        try
        {
            string csv = AjaxGrid.CreateCSV (stateObject.EndProc(result),gridId);
            AjaxGrid.OutputCsvFile(this.Response, csv, "export.csv");
        }
        finally
        {
            stateObject.Dispose();
        }
    }
    #endregion aynchronous I/O completion

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for 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 Dipl.-Ing. Rolf Cerff Software Development and IT-
Germany Germany
I'am a freelance software developer, located at Freiburg i. Br., Germany. My main domain is designing and developing Web applications based on the ASP.NET technology. Further main interests are. Sustainable and agile software architectures, Domain Driven Design (DDD). I'am a certified SCRUM Master.

Comments and Discussions