Wednesday, August 25, 2010

How to select Gridview Row in JavaScript

<script language="javascript" type="text/javascript">
var oldgridSelectedColor;

function setMouseOverColor(element)
{
    oldgridSelectedColor = element.style.backgroundColor;
    element.style.backgroundColor='yellow';
    element.style.cursor='hand';
    element.style.textDecoration='underline';
}

function setMouseOutColor(element)
{
    element.style.backgroundColor=oldgridSelectedColor;
    element.style.textDecoration='none';
}
</script>
protected void GridView1_RowDataBound(object sender, 
                    GridViewRowEventArgs e)
    {
        //check the item being bound is actually a DataRow, if it is, 
        //wire up the required html events and attach the relevant JavaScripts
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = 
                "javascript:setMouseOverColor(this);";
            e.Row.Attributes["onmouseout"] = 
                "javascript:setMouseOutColor(this);";
            e.Row.Attributes["onclick"] = 
            ClientScript.GetPostBackClientHyperlink
                (this.GridView1, "Select$" + e.Row.RowIndex);
        }
    }

No comments:

Post a Comment