GridView Row Fading Effect
By AzamSharp
Views: 13656

Introduction:

 

In any application it is always a good idea to give the user visual indication while some operation is in process. In this article I will describe how to give the visual indication while the user is performing operations on 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 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 the DIV elements contained in the row. The DIV elements are retrieved so 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 milli-seconds 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); 

 

The effect can be seen in the animation below:

 

Hope you enjoy the article!

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: Good Work!!!
Name: karbagamoorthy
Date: 6/19/2007 4:24:14 AM
Comment:
Good Work!!! Share more techincal point in asp.net such as tab control,video control,Pop-up window using master page concept..

Thanks& for Advance,

D.Karbagamoorthy
Subject: good
Name: Tariq yousef Ahmad
Date: 7/10/2007 1:49:53 AM
Comment:
its simple and more efficient
Subject: purpose
Name: someone
Date: 7/10/2007 7:53:30 AM
Comment:
Being a UI designer for over 10 years the idea behind this is good but there is one thing to take into consideration. You don't want to cause a shift in page content which you do in the grid. It would be better not to shift the grid (hide & show). Otherwise, it is a good idea as your eyes are already focused in the area you clicked and being notified about what is happening is great.
Subject: RE: purpose
Name: AzamSharp
Date: 7/10/2007 8:39:01 AM
Comment:
Thanks for the tip!
Subject: Feedback
Name: Ben
Date: 7/13/2007 4:39:16 PM
Comment:
I'm new to ASP.NET and so I'm not sure where I should place the code, it might be a good idea (maybe also not for me only) to also add where you should place to code.

Thanks!
Subject: RE: Feedback
Name: AzamSharp
Date: 7/13/2007 4:48:49 PM
Comment:
Hi Ben,

Well, the C# code is placed in the code behind. The purpose is to simply populate the GridView control.

The
The main code is the JavaScript Save function which you need to put either in the .ASPX code (HTML VIEW) or the .JS file.

So you can make JavaScript script tags and put the Save function inside the scripts tags.


Subject: Feedback 2
Name: Ben
Date: 7/13/2007 5:04:46 PM
Comment:
I'm sorry for again asking something but after I placed the code (behind code and asp page) I some kind of other problem, After I run it all that I see it an empty page! maybe I had some kind of problem connecting to the code that populate the grid view control or maybe I don't have "Northwind" DB on my PC.

Can you post a link that says how I connect the grid view to the DB.

Thanks ;-)
Subject: Need more information
Name: Shahnawaz
Date: 7/17/2007 10:11:43 PM
Comment:
Well good work so far!

you said that:
// Here you can also call the server side method to save the item to the database

My question is how can I call server side method right from javascript code.

Can you show me the example, it would be great.

Thanks!
Subject: RE: Need more information
Name: AzamSharp
Date: 7/19/2007 7:53:40 AM
Comment:
Hi Shahnawaz,

You can call the Server Side method using JavaScript with the help of AJAX. There are couple of different ways to perform this task. You can use ASP.NET AJAX PRO Library, Anthem Library or if you are using ASP.NET 2.0 then you can use Client Callbacks.
Subject: Great article
Name: VoyageMap.com
Date: 7/30/2007 7:53:45 AM
Comment:
Seamless interaction, good article
Subject: article
Name: laurent
Date: 7/31/2007 1:41:41 PM
Comment:
nice article.

Laurent
www.ooholidays.com
Subject: its fine
Name: jayesh banker
Date: 8/19/2007 10:50:14 PM
Comment:
its fine..
i want girdview control with row having the event mouseover and mouseout

and i want round shape border on each row of grid
Subject: Adding a roe effect
Name: yaip
Date: 11/17/2007 11:48:23 AM
Comment:
I use FooterRow to add a new row. I want to have similar fading effect for a newly added row to give the user a visual clue. How can I achieve this?
Subject: GridviewFading Effect
Name: Sujith
Date: 2/7/2008 11:23:13 PM
Comment:
Really nice one
Subject: nice
Name: ivan
Date: 3/3/2008 8:07:55 AM
Comment:
very nice..it helped

Ivan,
visit-croatia-holidays.com
Subject: Thanks
Name: Sam
Date: 5/5/2008 1:42:57 AM
Comment:
thanks alot



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008