ASP.NET gridview行onclick

我试图在数据绑定到gridview webcontrol后将onclick事件添加到一行。 下面的代码不添加任何属性(一旦创建页面就检查了viewsource),当然,没有添加任何function。 现在,我刚刚对页面进行了onclick打印,但最终它将链接到另一个页面。 关于什么是错的任何想法?

此外,感谢stackoverflow社区。 这个社区一直都是一个很好的帮助。 计划在本周末自己浏览一些post并开始回答问题,因为我可以回馈一下。

C#服务器端

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客户端

 function rowClicked(counter){ document.write(counter); } 

我在GridView的RowDataBound中使用它,添加属性以选择行:

 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 } } 

这是为了在用户点击行时捕获de事件:

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

要在jQuery中执行此操作,只需获取行单击事件,如下所示:

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

有了它,我建议将TR ID设置为行中显示的对象的主键。

Grid是否设置为调用dataTbl_RowDataBound事件? 如果在该事件中使用断点进行调试,该事件是否会被触发?

如果它不起作用,您可以使用rowcommand事件。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx