This article
will show you how to display mouseover effect in webgrid rows using only css.
In this there is no use of jquery or JavaScript code to show the highlight effect.
Some of my
previous articles are as follows: Asp.Net
MVC : WebGrid Image Binding With Shorting Data Using C#.Net, Asp.Net
MVC : WebGrid Image Binding Using C#.Net.
So for this article first we will create a new mvc application and add the below model class code.
So for this article first we will create a new mvc application and add the below model class code.
public class CountryModel
{
public List<Country> CountryList {
get; set; }
}
public class Country
{
public string CountryName { get; set; }
public int Population { get; set; }
public string FlagURL { get; set; }
}
|
Now check
the code of controller.
//
// GET: /Home/
[HttpGet]
public ActionResult
Index()
{
CountryModel
_countryModel = new CountryModel();
_countryModel.CountryList= GetCountryList();
return
View(_countryModel);
}
///
/// Country list
///
public List<Country> GetCountryList()
{
List<Country> _country = new List<Country>();
_country.Add(new Country { CountryName = "India", Population = 124000, FlagURL = "CountryFlag/india.png" });
_country.Add(new Country { CountryName = "China", Population = 84545, FlagURL = "CountryFlag/china.png" });
_country.Add(new Country { CountryName = "Germany", Population = 9354999, FlagURL = "CountryFlag/germany.png" });
_country.Add(new Country { CountryName = "Japan", Population = 24999, FlagURL = "CountryFlag/japan.png" });
return _country;
}
|
In above
code I have prepared the code to pass the data to model from controller to
view. Now we will create the view and add the below code to bind and styling
the grid view.
@model MVC_Demos.Models.CountryModel
@{
ViewBag.Title
= "How to Display Mouseover Effect
in WebGrid Rows Using Only CSS";
}
<style type="text/css">
.gridtable {
font-family: verdana,arial,sans-serif;
font-size: 11px;
color: #333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
.gridtable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
.gridtable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
.gridtable tr:hover td,
.gridtable tr.even:hover td.active,
.gridtable tr.odd:hover td.active {
background: #b6ff00;
}
</style>
@using (Html.BeginForm("Index", "Home"))
{
<table width="100%" cellpadding="5" cellspacing="2" border="0" style="background-color: White;">
<tr>
<td>
@{
var grid = new WebGrid(source:
Model.CountryList,
rowsPerPage:
10,
canSort: true);
}
@grid.GetHtml(
tableStyle: "gridtable",
alternatingRowStyle: "even",
columns: grid.Columns(
grid.Column("CountryName", "CountryName"),
grid.Column("Population", "Population"),
grid.Column("FlagURL", header: "Country
Flag", format: @
))
</td>
</tr>
</table>
}
|
In above
code please check the highlighted part of the code of css style. This code us
responsible for highlighting the row on mouse over. Now we have done run the application to check
the output.
0 comments:
Please let me know your view