ASP.NET onclick grid line

I am trying to add an onclick event to a string as soon as the data is bound to a webview. In the code below, no attributes are added (the source of the views is checked after the page is created), and, of course, the functionality is not added. Right now, I just got an onclick print on the page, but it will eventually link to another page. Any ideas on what's wrong?

Also thanks to the stackoverflow community as a whole. This community has always been of great help. Plan to go through some posts this weekend and start answering questions of how I can give a little.

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+"')"); } 

Client side Javascript

 function rowClicked(counter){ document.write(counter); } 
+8
javascript c # gridview onclick
source share
4 answers

I use this in the RowDataBound of my GridView to add an attribute to select a row:

 protected void grvSearch_RowDataBound(object sender, GridViewRowEventArgs e) { try { switch (e.Row.RowType) { case DataControlRowType.Header: //... break; case DataControlRowType.DataRow: e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#93A3B0'; this.style.color='White'; this.style.cursor='pointer'"); if (e.Row.RowState == DataControlRowState.Alternate) { e.Row.Attributes.Add("onmouseout", String.Format("this.style.color='Black';this.style.backgroundColor='{0}';", grvSearch.AlternatingRowStyle.BackColor.ToKnownColor())); } else { e.Row.Attributes.Add("onmouseout", String.Format("this.style.color='Black';this.style.backgroundColor='{0}';", grvSearch.RowStyle.BackColor.ToKnownColor())); } e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(grvSearch, "Select$" + e.Row.RowIndex.ToString())); break; } } catch { //...throw } } 

And this is to catch the event when the user clicks on the line:

 protected void grvSearch_SelectedIndexChanged(object sender, EventArgs e) { try { //Do wherever you want with grvSearch.SelectedIndex } catch { //...throw } } 
+9
source share

To do this in jQuery, just get the click event for the string as follows:

 $(document).ready(function () { var clickCnt = 0; $('table tr').click(function(){ clickCnt++; //Do something }); }); 

In doing so, I recommend binding the TR ID to the primary key to the object that is displayed in the row.

+3
source share

Is a Grid to call the dataTbl_RowDataBound event? If you are debugging a breakpoint in this event, does this event fire?

+3
source share
+2
source share

All Articles