ASP.net GridView: get LinkItem's row

北城以北 提交于 2020-01-06 14:28:23

问题


I want to show "Delete" link in GridView to registred users, therefore I am using 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>

Now in my deleteEntry() function how can I know anything about the row in which "Delete" link was clicked? How to ge for e.g. rowindex?


回答1:


You could approach this slightly different. You see, when a control is placed inside a gridview, any event raised from that control raises also the RowCommand on the GridView.

To get what you want you could then add both CommandName and CommandArgument to your LinkButton and then catch it in the GridView's RowCommand.

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

where myObjectID is the name of the ID column of your object you bind the grid to.

Then

void GridView1_RowCommand( object sender, GridViewCommandEventArgs e )
{
    if ( e.CommandName == "LinkButtonClicked" )
    {
        string id = e.CommandArgument; // this is the ID of the clicked item
    }
}


来源:https://stackoverflow.com/questions/9655305/asp-net-gridview-get-linkitems-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!