Вы можете использовать RowDataBound .Свойство строка содержит индекс строки
Код позади
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
((HyperLink)e.Row.FindControl("hlk_Bookname"))
.NavigateUrl=string.Format("javascript:doYouWantTo({0})",e.Row.RowIndex));
}
}
ASPX
<asp:gridview id="GridView1"
onrowdatabound="GridView1_RowDataBound"
......
Редактировать
Если есть лучшее решение для вашей проблемы.Я думаю, что вы пытаетесь снова изобрести колесо.Я думаю, что вы можете взглянуть на RowCommand событие.Вы можете использовать его в сочетании с RowCreated .Вы можете увидеть пример здесь .Или вы можете сделать это примерно так:
Код позади
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Add")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = ContactsGridView.Rows[index];
//What ever code you want to do....
}
}
//Set the command argument to the row index
protected void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
addButton.CommandArgument = e.Row.RowIndex.ToString();
}
}
ASPX
<asp:gridview id="GridView1"
onrowcommand="GridView1_RowCommand"
OnRowCreated="GridView1_RowCreated"
runat="server">
<columns>
<asp:buttonfield buttontype="Link"
commandname="Add"
text="Add"/>
Надеюсь, чтопомощь ..