GridView Component had brought some really nice features to the ASP.Net development. And even those features that it lacks can be easily added via customization or inheritance.
One of the things that GridView lacks are all those client-side bells and whistles that make todays web users happy (or miserable, depending on how developers over-use them ).
For example, very often it is required to highlight the data rows when mouse pointer is over the row (Mouse Hover effect).
This is not something that GridView does by default, but as i said, it is very easy to add new features to the GridView.
We can add few lines of JavaScript and thats it.
But to create this effect in a way as it should be done, a little planning is necessary:
If you ask the almighty Google, it will direct you to the various implementations of this effect for GridView but most of them do not take into account GridView's ItemStyle and AlternatingItemStyle properties and just override them when changing row colors.
What we are going to do is to implement mouse hover row highlighting with respect of all GridView properties, and restoring their original values when the mouse pointer is moved away from the rows/GridView.
In order to highlight the row on mouse hover, we need to add some custom JavaScript code to onmouseover and onmouseout event handlers of the rows of our GridView.
We can do this by placing this code to the RowCreated event handler of our GridView:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// only apply changes if its DataRow
if (e.Row.RowType == DataControlRowType.DataRow)
{
// when mouse is over the row, save original color to new attribute, and change it to highlight yellow color
e.Row.Attributes.Add("onmouseover",
"this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#EEFF00'");
// when mouse leaves the row, change the bg color to its original value
e.Row.Attributes.Add("onmouseout",
"this.style.backgroundColor=this.originalstyle;");
}
}
What this code actually does?
RowCreated event is fired immediately after each row is created in our GridView. So for each created row, we first check if its a DataRow (not Header of Footer for example) and if it is, we add the JavaScript code via Add method of the Attributes collection (AttributeCollection type) that has name-value pairs of the rendering attributes of the GridViewRow.
In onmouseover handler code, we first store (for later restoring) the original background color of the row in a custom attribute we called "originalstyle" and we then change it to a custom color (light Yellow in this case).
In onmouseout handler, we restore the original background color of the row from the "originalstyle" attribute.
And that is it. If you bind this GridView with some data and display it on a page, when you move your mouse pointer over any of the rows, it will change its background color into Yellow. And when you move mouse out of the row, the original background color of the row is restored.
IMPORTANT: This works correctly even if you have set the AlternatingRowStyle of the GridView and even if the row was previously selected (this detail is something that is missing in all other implementations of this GridView behavior we have seen on the internet).
Ok, now that we solved this, why not make a custom WebControl out of it by inheriting the original ASP.NET GridView control and adding highlighting feature to it?
Its really simple, here is the code of the HoverGridView custom WebControl:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
/// <summary>
/// Summary description for HoverGridView
/// </summary>
namespace Roboblob.WebControls
{
[ToolboxData("<{0}:HoverGridView runat=server></{0}:HoverGridView>")]
public class HoverGridView : System.Web.UI.WebControls.GridView
{
public bool MouseHoverRowHighlightEnabled
{
get
{
if (ViewState["MouseHoverRowHighlightEnabled"] != null)
return (bool)ViewState["MouseHoverRowHighlightEnabled"];
else
return false;
}
set { ViewState["MouseHoverRowHighlightEnabled"] = value; }
}
public Color RowHighlightColor
{
get
{
if (ViewState["RowHighlightColor"] != null)
return (Color)ViewState["RowHighlightColor"];
else
{
// default color
return Color.Yellow;
}
}
set { ViewState["RowHighlightColor"] = value; }
}
protected override void OnRowCreated(GridViewRowEventArgs e)
{
base.OnRowCreated(e);
if (MouseHoverRowHighlightEnabled)
{
// only apply changes if its DataRow
if (e.Row.RowType == DataControlRowType.DataRow)
{
// when mouse is over the row, save original color to new attribute
// and change it to highlight yellow color
e.Row.Attributes.Add("onmouseover",
string.Format("this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='{0}'",
ColorTranslator.ToHtml(RowHighlightColor)));
// when mouse leaves the row, change the bg color to its original value
e.Row.Attributes.Add("onmouseout",
"this.style.backgroundColor=this.originalstyle;");
}
}
}
}
}
And here is how to use this custom WebControl on a WebForm:
First we must register it on the top of our aspx page:
<%@ Register Namespace="Roboblob.WebControls" TagPrefix="roboblob" %>
And when we place our custom GridView on the form here is how its declaration looks like:
<roboblob:HoverGridView runat="server" MouseHoverRowHighlightEnabled="True" RowHighlightColor="Green" ID="HoverGridView1" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
</Columns>
</roboblob:HoverGridView>
As you can see its really only a few lines of code, and yet we have created a custom WebControl that we can place on any WebForm, switch the highlighting on/off, change the highlight color etc.
This is the beauty of creating and using WebControls and Controls in general...
We could add many other features to our custom GridView, like different colors for alternating rows, for selected rows etc but i will leave that as something for you to play with.
No comments:
Post a Comment