0

我想在 GridView 中向注册用户显示“删除”链接,因此我使用的是 templateField:

    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" OnSorting="GridView_Sort">
    <Columns>
        <asp:TemplateField HeaderText="Control">
        <ItemTemplate>
            <asp:LinkButton ID="LinkButton1" runat="server" onClick="deleteEntry()"  Text="Delete"></asp:LinkButton>
        </ItemTemplate>
        </asp:TemplateField>  
    </Columns>
    </asp:GridView>

现在在我的 deleteEntry() 函数中,我如何知道单击“删除”链接所在的行的任何信息?如何获取例如rowindex?

4

1 回答 1

1

你可以处理这个略有不同。您会看到,当将控件放置gridview 中时,从该控件引发的任何事件也会RowCommand在 GridView 上引发。

为了得到你想要的,你可以将两者都添加CommandNameCommandArgument你的LinkButton,然后在 GridView 的 RowCommand 中捕获它。

<asp:LinkButton id="LinkButton1" runat="server" commandName="LinkButtonClicked" commandArgument='Eval("myObjectID")' />

myObjectID您将网格绑定到的对象的 ID 列的名称在哪里。

然后

void GridView1_RowCommand( object sender, GridViewCommandEventArgs e )
{
    if ( e.CommandName == "LinkButtonClicked" )
    {
        string id = e.CommandArgument; // this is the ID of the clicked item
    }
}
于 2012-03-11T13:31:27.330 回答