5,448,416 members and growing! (18,081 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

GridView Row Fading Effect

By azamsharp

An article on how to give visual indication while the user is performing operations on a selected row
C#, Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 6 Jul 2007
Updated: 6 Jul 2007
Views: 16,855
Bookmarked: 21 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.65 Rating: 3.40 out of 5
1 vote, 16.7%
1
1 vote, 16.7%
2
0 votes, 0.0%
3
1 vote, 16.7%
4
3 votes, 50.0%
5

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

About the Author

azamsharp


I am the founder of knowledge base website, GridViewGuy, RefactorCode.com and ScreencastADay.com. GridViewGuy hosts all of my articles, videos and podcasts.

ScreencastADay is a website committed to educate you everyday. The idea of the website is really simple. We believe that in order to be successful you need to learn something new everyday. ScreencastADay puts this thought into action by providing new screencast every day. This means you will get 7 screencasts a week and 365 screencasts a year unless of course it is a leap year in that case you get one more bonus screencast.

ScreencastADay.com: www.ScreencastADay.com

RefactorCode: www.RefactorCode.com

GridViewGuy: GridViewGuy

My Blog:

Blog

Occupation: Web Developer
Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionI want to save datamembervrun22:46 7 Sep '07  
GeneralAdd row below saved row?memberLumberjackchester5:12 18 Jul '07  
GeneralRe: Add row below saved row?memberazamsharp11:00 18 Jul '07  
GeneralRe: Add row below saved row?memberLumberjackchester22:42 18 Jul '07  
GeneralRe: Add row below saved row?memberazamsharp7:02 19 Jul '07  
GeneralRe: Add row below saved row?memberazamsharp7:03 19 Jul '07  
GeneralRe: Add row below saved row?memberazamsharp7:03 19 Jul '07  
GeneralRe: Add row below saved row?memberLumberjackchester23:57 19 Jul '07  
GeneralRe: Add row below saved row?memberLumberjackchester23:58 19 Jul '07  
QuestionThank youmemberHoomanG20:24 11 Jul '07  
AnswerRe: Thank youmemberazamsharp6:56 12 Jul '07  
GeneralNice subject!memberDarkjo21:22 9 Jul '07  
QuestionFade Out!!!!memberH.Riazi20:32 6 Jul '07  
AnswerRe: Fade Out!!!!memberazamsharp16:16 7 Jul '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Jul 2007
Editor: Genevieve Sovereign
Copyright 2007 by azamsharp
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project