ASP.net GridView:获取LinkItem的行

时间:2012-03-11 13:22:43

标签: asp.net gridview templatefield

我想在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位置?

1 个答案:

答案 0 :(得分:1)

你可以稍微改变一下。你看,当一个控件放在里面一个gridview时,从该控件引发的任何事件都会引发GridView上的RowCommand

要获得您想要的内容,您可以将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
    }
}