16 Apr 2007

GridView Row-Click

A simple way to add "select on row-click" functionality to GridView without causing Event validation errors:


protected override void OnRowCreated(GridViewRowEventArgs e)
{
base.OnRowCreated(e);

if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onClick"] = "this.style.cursor = 'wait';"
+ Page.ClientScript.GetPostBackEventReference(this, "Select$" + e.Row.RowIndex.ToString(), true);
}
}


And how do you handle the click? Just hook up to the GridView's SelectedIndexChanged event handler and check the GridView.SelectedValue property.

ps. for row-hover and striping fun, why not use JQuery? Just implement the grid's RowStyle.CssClass (e.g. set it as 'zootGridRow') and set some style directives to handle the following classes...


$(function() {

// Implement grid-row striping
$('#myGrid .zootGridRow:odd').addClass('zootGridAlternateRow');

// Implement grid-row hover animation
$('#myGrid .zootGridRow').hover(function(){$(this).addClass('zootGridRowHover')},
function(){$(this).removeClass('zootGridRowHover')});

});

No comments:

Post a Comment

Comments are very welcome but are moderated to prevent spam.

If I helped you out today, you can buy me a beer below. Cheers!