Tuesday, August 25, 2009

Row Click Event in Grid View

Hi actually there is no row click event in gridview for this one using javascript we can solve this one.

step one
--------

In grid view row databound event
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow tr = e.Row;
tr.Attributes.Add("onClick", "javascript:selectMe(this);");
tr.Attributes.Add("style", "cursor:hand;");

}
}
catch
{
}
}

in this one we r adding javascript event to gridview row. same process we can do in another way also if you know jquery using jquery also we can add attribute to every row in gridview

now defining javascript function selectMe(obj).actually i'm getting the all columns values and alerting all values.in my gridview i have 3 columns for every row
script type="text/javascript"
fun ction selectMe(obj)
{
var name = obj.cells[0].firstChild.data;
var email = obj.cells[1].firstChild.data;
var number = obj.cells[2].firstChild.data;
alert("name =" + name + ",Email=" + email +",Number=" +number);
}
/script