1

我尝试了很多组合以便能够在下面的代码中获得 rowIndex,应该在“这是我想通过 ROWINDEX 的地方”部分下面写什么。

            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
                AutoGenerateColumns="False" DataKeyNames="Id,BookName" DataSourceID="SqlDataSource1"
                Width="800px" CssClass="Gridview">
                <Columns>
                    <asp:TemplateField HeaderText="BookName" SortExpression="BookName" ItemStyle-Width="250px">
                        <ItemTemplate>
                            <asp:HyperLink ID="hlk_Bookname" runat="server" Enabled='<%# !Convert.ToBoolean(Eval("Reserve")) %>'
                                Text='<%# Bind("BookName") %>' NavigateUrl='javascript:doYouWantTo("THIS IS WHERE I WANT TO PASS ROWINDEX ")'></asp:HyperLink>
                        </ItemTemplate>                            
                    </asp:TemplateField>

.. .. ..

4

1 回答 1

1

您可以使用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"/>

希望这有帮助..

于 2012-03-16T07:39:04.450 回答