I have creat开发者_如何学Ced a gridview which adds a remove button through item template. I wish to know how do u remove the row from grid view dynamically depending upon the button he clicks ?
protected void RemoveBtn_OnClick(object sender, EventArgs e)
{
Button clickedButton = sender as Button;
GridViewRow row = (GridViewRow)clickedButton.Parent.Parent;
int rowID = Convert.ToInt16(row.RowIndex);
GridView1.DeleteRow(rowID);
}
<asp:GridView ID= "GridView1" runat="server" AutoGenerateColumns="true" OnRowDeleting="RowDeletingEvent">
<Columns>
<asp:TemplateField HeaderText="Remove Items">
<ItemTemplate>
<asp:Button id="RemoveBtn" runat="server" Text="Remove" OnClick="RemoveBtn_OnClick"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Thank you in anticipation
Use the GridView.RowCommand Event.
The RowCommand event is raised when a button is clicked in the GridView control. This enables you to provide an event-handling method that performs a custom routine whenever this event occurs.
Buttons within a GridView control can also invoke some of the built-in functionality of the control. To perform one of these operations, set the CommandName property of a button to one of the values in the following table.
CommandName Value "Delete" - Deletes the current record. Raises the RowDeleting and RowDeleted events.
精彩评论