Click here to Skip to main content
12,354,716 members (61,621 online)
Click here to Skip to main content

Stats

22.3K views
618 downloads
45 bookmarked
Posted

Selectable GridView with WaitBox

, 16 Oct 2008 CPOL
This article describes how to implement a selectable GridView control in ASP.NET.
WaterGrid
App_Code
NewsItems.xss
App_Data
Bin
AjaxControlToolkit.dll
Images
bar_bk.gif
bar_line.gif
bg_nav.gif
Thumbs.db
PreRequisites
AjaxControlToolkit.dll
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)

Share

About the Author

Iwona Les
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.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 16 Oct 2008
Article Copyright 2008 by Iwona Les
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid