Click here to Skip to main content
12,450,981 members (54,243 online)
Click here to Skip to main content
Add your own
alternative version

Stats

46.3K views
38 bookmarked
Posted

GridView Row Fading Effect

, 6 Jul 2007
Rate this:
Please Sign up or sign in to vote.
An article on how to give visual indication while the user is performing operations on a selected row

Introduction

In any application, it is always a good idea to give the user visual indication while some operation is in progress. In this article, I will describe how to give visual indication while the user is performing operations on a selected row.

Populating the GridView control

The first task is to populate the GridView control. Take a look at the code below, which is used to populate the GridView.

private void BindData()
{
    SqlConnection myConnection = 
        new SqlConnection(
        "Server=localhost;Database=Northwind;Trusted_Connection=true");
    SqlDataAdapter ad = 
        new SqlDataAdapter("SELECT * FROM Categories", myConnection);
    DataSet ds = new DataSet();
    ad.Fill(ds);
    gvCategories.DataSource = ds;
    gvCategories.DataBind();
}

The HTML code for the GridView looks like the following:

<asp:GridView ID="gvCategories" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField Visible="true">
            <ItemTemplate>
                <div id="categoryID"><%# Eval("CategoryID") %></div>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Literal ID="litCategoryName" 
                    runat="server" Text='<%# Eval("CategoryName") 
                    %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <input type="button" value="Save" onclick="Save(this)" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <div id="message" ></div>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

The important thing to note is that when the button inside the Template Column is clicked, the Save(this) method is called. Let's take a look at the Save method.

The Save method

The Save method is responsible for creating the fading effect. Let's take a look at the Save method and then we will discuss how it is implemented.

function Save(obj)
{
    var row = null;
    if(IsFireFox())
    {
        row = obj.parentNode.parentNode;
    }
    else
    {
        row = obj.parentElement.parentElement;
    }
    var message = row.getElementsByTagName("DIV"); 
    row.style.backgroundColor = 'Yellow'; 
    message[1].innerHTML = 'Saving!'; 
    // Here you can also call the server side method 
    // to save the item to the database
    window.setTimeout(function() 
    { 
        row.style.backgroundColor = 'White'; 
        message[1].innerHTML = 'Saved!'; 
    }, 2000); 
}

The first task is to get the row object of the GridView which was clicked. After getting the row object, I find all of the DIV elements contained in the row. The DIV elements are retrieved so that I can display the message while the row is being saved. The heart of the fading function is the window.setTimeOut method, which is fired after 2000 milliseconds or 2 seconds. Instead of creating an actual method, I am passing an anonymous method to the window.setTimeout function.

window.setTimeout(function() 
{ 
    row.style.backgroundColor = 'White'; 
    message[1].innerHTML = 'Saved!'; 
}, 2000);

You can view the live animation of the effect using the URL given below:

<a href="http://gridviewguy.com/ArticleDetails.aspx?articleID=241"></a>

History

  • 6 July, 2007 -- Original version posted

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

azamsharp
Web Developer
United States United States
I am the founder of knowledge base website, HighOnCoding, GridViewGuy, RefactorCode.com and ScreencastADay.com.

HighOnCoding is a website which will get you high legally with useful information. There are tons of articles, videos and podcasts hosted on HighOnCoding.

HighOnCoding.com www.HighOnCoding.com


My Blog:

Blog



Buy my iPhone app ABC Pop

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionI want to save data Pin
vrun7-Sep-07 21:46
membervrun7-Sep-07 21:46 
QuestionAdd row below saved row? Pin
Lumberjackchester18-Jul-07 4:12
memberLumberjackchester18-Jul-07 4:12 
AnswerRe: Add row below saved row? Pin
azamsharp18-Jul-07 10:00
memberazamsharp18-Jul-07 10:00 
GeneralRe: Add row below saved row? Pin
Lumberjackchester18-Jul-07 21:42
memberLumberjackchester18-Jul-07 21:42 
GeneralRe: Add row below saved row? Pin
azamsharp19-Jul-07 6:02
memberazamsharp19-Jul-07 6:02 
GeneralRe: Add row below saved row? Pin
azamsharp19-Jul-07 6:03
memberazamsharp19-Jul-07 6:03 
GeneralRe: Add row below saved row? Pin
azamsharp19-Jul-07 6:03
memberazamsharp19-Jul-07 6:03 
GeneralRe: Add row below saved row? Pin
Lumberjackchester19-Jul-07 22:57
memberLumberjackchester19-Jul-07 22:57 
GeneralRe: Add row below saved row? Pin
Lumberjackchester19-Jul-07 22:58
memberLumberjackchester19-Jul-07 22:58 
QuestionThank you Pin
HoomanG11-Jul-07 19:24
memberHoomanG11-Jul-07 19:24 
AnswerRe: Thank you Pin
azamsharp12-Jul-07 5:56
memberazamsharp12-Jul-07 5:56 
QuestionFade Out!!!! Pin
H.Riazi6-Jul-07 19:32
memberH.Riazi6-Jul-07 19:32 
AnswerRe: Fade Out!!!! Pin
azamsharp7-Jul-07 15:16
memberazamsharp7-Jul-07 15:16 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 6 Jul 2007
Article Copyright 2007 by azamsharp
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid