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 に答える