gridview如何动态获取行号?
int i = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != -1)
{
i = e.Row.RowIndex;
((HyperLink)e.Row.FindControl("HyperLinkEdit")).Attributes.Add("onclick", "window.showModalDialog('assignexercise.aspx?id=" + this.GridView1.DataKeys[e.Row.RowIndex].Value.ToString() + "','','width=610px,height=635px,top=40px,left=150px');");
}
}
原意是想要为gridview 的一个添加了hyperlink的模板列添加onclick事件,当运行时点击某一列可以获得该列的行号,赋值给i。如上这样赋值的确可以获得行号,但是如果点击了第n行的hyperlink,i仍旧是绑定时绑定的最后一行的行号。怎样写才能实现点击第n行的hyperlink将行号传给i?
------解决方案--------------------page_load里面加!ispostback了吗
------解决方案--------------------建议用linkbutton
<asp:LinkButton ID="lbtn" CommandName="link" runat="server" CommandArgument='<%#Eval("ID") %>'>link</asp:LinkButton>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e){
if (e.CommandName == "link")
{
Response.Redirect("assignexercise.aspx?id=" + e.CommandArgument.ToString());
}
}