Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 29771 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > How to display mouseover effect in GridView rows using only CSS?

How to display mouseover effect in GridView rows using only CSS?

6 vote(s)
Rating: 4.17 out of 5
Article posted by Itfunda on 5/25/2011 | Views: 10791 | Category: ASP.NET | Level: Intermediate | Points: 250 red flag


This article explains how to display mouseover effect in GridView rows using only CSS. In this article, we are not going to use jQuery or JavaScript.

Introduction

Here we have used CSS style class which defines the GridView’s row property that helps to display the hover effect when we will move mouse over that. The RowStyle-CssClass property of the GridView has been set as “rowHover”; when GridView renders on the page, each row of the GridView (GridView is rendered on the page as html table) is rendered with “rowHover” CSS class that only takes effect when we mouse hover on the GridView row.

Get solution hundreds of real time .NET How to’s like this, click here.

Steps to follow

Define following CSS style in the .css file or on the .aspx page itself.

ASPX Page:

 

<style type="text/css">

#GridView1 tr.rowHover:hover

{

background-color: Yellow;

font-family: Arial;

}

</style>

<asp:GridView ID="GridView1" runat="server" EnableViewState="false" RowStyle-CssClass="rowHover" ClientIDMode="Static" />

Note that the important thing here is the ID of the GridView. When GridView rendered on the page it’s id should be GridView1 that is why we have set ClientIDMode=static.

The CSS class name is rowHover with filtration that instruct the browser that this CSS class should be applied only to the element having id as “GridView1” with “tr” element having class=”rowHover” and only when user mouse over it.

In this CSS class we have specified the background color to “Yellow” and font family to “Arial”.

CODE Behind:

 

string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

 

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

GetData();

}

}


 

private void GetData()

{

DataTable table = new DataTable();

// get the connection

using (SqlConnection conn = new SqlConnection(_connStr))

{

// write the sql statement to execute

string sql = "SELECT AutoId, FirstName, LastName, Age, Active FROM PersonalDetail ORDER By AutoId";

// instantiate the command object to fire

using (SqlCommand cmd = new SqlCommand(sql, conn))

{

// get the adapter object and attach the command object to it

using (SqlDataAdapter ad = new SqlDataAdapter(cmd))

{

// fire Fill method to fetch the data and fill into DataTable

ad.Fill(table);

}

}

}

// specify the data source for the GridView

GridView1.DataSource = table;

// bind the data now

GridView1.DataBind();

}

In the above code snippet, the code behind code is just to populate records from the database to the GridView.

Now when we run this page, we get the output as shown below. When user mouse hover the background color of the rows are changed to Yellow and font is changed to Arial.

Conclusion

By setting the RowStyle-CssClass property to filtered CSS class, we can display mouseover effect on GridView rows.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About IT Funda

Experience:0 year(s)
Home page:http://www.itfunda.com/
Member since:Saturday, May 21, 2011
Level:Starter
Status: [Member]
Biography:Get .NET How to | jQuery How to.
 Responses
Posted by: A4u6178 | Posted on: 26 May 2011 05:35:59 AM | Points: 25

very nice article, thanks for sharing this article.

Posted by: A4u6178 | Posted on: 26 May 2011 05:36:03 AM | Points: 25

very nice article, thanks for sharing this article.

Posted by: Susanthampy | Posted on: 26 May 2011 11:20:34 PM | Points: 25

Good

Posted by: Prabhakar | Posted on: 27 May 2011 05:11:34 AM | Points: 25

Gud one Article. . . it's a gud thing to . . Designing Purpose . .

Posted by: Tripati_tutu | Posted on: 27 May 2011 05:34:11 AM | Points: 25

Looking nice and simple to understand...

Thanks ITFunda for sharing.

Posted by: Ksuresh | Posted on: 27 May 2011 07:02:44 AM | Points: 25

Very good Article,Thanks for sharing.

>> Write Response - Respond to this post and get points
Related Posts

These code can insert image in database using sqldatasource.

To list values of all sever environment variables in one go.

In this section we will run through a quick FAQ for WCF. I am sure after reading this you will get a good understanding of the fundamentals of WCF.

Adding dynamic field in to the gridview.

To do paginations to display large number of records on the page using GridView and Display a custom message when no records to display in the GridView, we can follow this approach.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/28/2012 11:54:57 AM