I have a GridView with some information in it. When I click on one of the rows, I want to send its ClientId to JavaScript.
C# code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("onclick", "javascript:ChangeRowColor('" + e.Row.ClientID + "')");
}
JavaScript:
<script type="text/javascript">
function ChangeRowColor(row) {
alert(row);
</script>
but the problem is that when I click on a row it returns "gridview1", and that isn’t the row’s ID.
How can I send a GridView row ID to JavaScript?
Use the
thiskeyword. In the context of your function,thiswill refer to the row.And then access the id by using the id attribute:
Additional Information
The
javascript:prefix is unnecessary in this context.