ste*_*ris 9 javascript c# asp.net gridview onclick
一旦数据绑定到gridview webcontrol,我正在尝试将onclick事件添加到一行.下面的代码不添加任何属性(在创建页面后检查viewsource),当然,没有添加任何功能.现在,我刚刚对页面进行了onclick打印,但最终它将链接到另一个页面.关于什么是错的任何想法?
此外,感谢stackoverflow社区.这个社区一直都是一个很好的帮助.计划在本周末自己查看一些帖子并开始回答问题,因为我可以回馈一下.
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+"')");
}
Run Code Online (Sandbox Code Playgroud)
Javascript客户端
function rowClicked(counter){
document.write(counter);
}
Run Code Online (Sandbox Code Playgroud)
小智 10
我在我的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
}
}
Run Code Online (Sandbox Code Playgroud)
这是为了在用户点击该行时捕获de事件:
protected void grvSearch_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
//Do wherever you want with grvSearch.SelectedIndex
}
catch
{
//...throw
}
}
Run Code Online (Sandbox Code Playgroud)