I'm attempting to have an onclick event added to a row once the data is bound to a gridview webcontrol. The code below is not adding any attributes (checked the viewsource once the page is created) and, of course, no functionality is added. Right now, I've just got the onclick printing to the page, but eventually it will link to another page. Any ideas on what's wrong?
Also, thanks to the stackoverflow community at large. This community has always been a great help. Plan to go through some posts myself this weekend and start answering questions as I can to give back a bit.
C# server-side
protected void dataTbl_RowDataBound(GridViewRowEventArgs e){
e.Row.Attributes.Add("id",e.Row.Cells[0].Text);
e.Row.Attributes.Add("onclick", "rowClick('"+e.Row.RowIndex+"')");
}
Javascript client-side
function rowClicked(counter){
document.write(counter);
}
Is the
Grid
set up to calldataTbl_RowDataBound
event? If you debug with a breakpoint in that event, does that event get fired?You can use rowcommand event if it's not working. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
To do this in jQuery, simply get the row click event like this:
With that, I recommend setting the TR ID to the primary key to the object that is displayed in the row.
I'm using this in the RowDataBound of my GridView, to add the attribute to select the row:
And this to catch de event when an user click the row: