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

Selectable GridView with WaitBox

Rate me:
Please Sign up or sign in to vote.
3.89/5 (9 votes)
16 Oct 2008CPOL2 min read 31.3K   652   45  
This article describes how to implement a selectable GridView control in ASP.NET.
using System;
using System.Data;
using System.Configuration;
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;

public partial class _Default : System.Web.UI.Page
{
    #region properties
    /// <summary>
    /// Gets or sets the app mode. It's necessary to distinguish the current state.
    /// Because normally there is an 'click-event' attached to the datagrid. If user 
    /// decide to describe an filter, app mode will be switched to Common.Mode.Describe
    /// and no click-event will be registered.
    /// </summary>
    /// <value>The current application mode.</value>
    public Mode CurrentMode
    {
        get
        {
            object sessionObj = Session[MODE];
            return (sessionObj != null) ? (Mode)sessionObj : Mode.New;
        }
        set { Session[MODE] = value; }
    }
    #endregion

    /// <summary>
    /// Colorize the pop-menu items. Handles the Load event of the LinkBtn control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void LinkBtn_Load(object sender, EventArgs e)
    {
        ((LinkButton)sender).Attributes["onmouseover"] = "this.style.color='Red';";
        ((LinkButton)sender).Attributes["onmouseout"] = "this.style.color='Blue';";
    }


    /// <summary>
    /// On RowDataBound register the java script (click-event) for the grid view.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
    protected void RegisterJavaScript(object sender, GridViewRowEventArgs e)
    {
        if (CurrentMode != Mode.Describe)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';";
                e.Row.Attributes.Add("onclick",
                Page.ClientScript.GetPostBackEventReference((Control)sender, "Select$" + e.Row.RowIndex.ToString()));
            }
        }
    }

    /// <summary>
    /// Register click-event 
    /// </summary>
    /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"></see> that receives the page content.</param>
    protected override void Render(HtmlTextWriter writer)
    {
        if (CurrentMode != Mode.Describe)
            for (int i = 0; i < this.gridFilters.Rows.Count; i++)
                ClientScript.RegisterForEventValidation(this.gridFilters.UniqueID, "Select$" + i);
        base.Render(writer);
    }

    /// <summary>
    /// Perform an action depending on selected item.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCommandEventArgs"/> instance containing the event data.</param>
    protected void gridFilters_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        CurrentMode = Mode.New;

        switch (e.CommandName)
        {
            case ("Insert"):
                upMain.Update();
                break;
            case ("Edit"):
                CurrentMode = Mode.Describe;
                break;
        }
    }

    #region variables
    public enum Mode { New, Save, Load, None, Describe };
    public const string MODE = "APPLICATION_MODE";
    #endregion
}

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 (Junior) Artaker Computersysteme GmbH
Austria Austria
I've began programming at the Warsaw University of Technolog. Currently I'm trying hardly;) to finish a master program at Vienna University of Technology and at the same time I'm working as Software Developer.
I'm interested in Web Development, technologies like AJAX, Silverlight, Semantic Web etc.

Comments and Discussions