What is Gridview_SelectedIndexChanged and Gridview_RowCommand events in ASP.Net?

 Posted by vishalneeraj-24503 on 11/20/2013 | Category: ASP.NET Interview questions | Views: 1354 | Points: 40
Answer:

Gridview_SelectedIndexChanged -> For working with this event, we have to give CommandName = "Select" in Command/Link/Hyperlink/Image Button in ItemTemplate in Gridview.

Gridview_RowCommand -> For working with this event, we can give any name to CommandName property as CommandName = "View_Details" or CommandName = "Add_details" or we can give any name.

Aspx page will look like this:-

<asp:Gridview id="grid_employee" runat="server" AllowPaging="True" AutoGeneratedColumns = "False">

<Command>
<asp:TemplateField>
<ItemTemplate>
<asp:Button id="btn_select" runat="Server" CommandName="Select" Text="View"></asp:Button>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:Button id="btn_view" runat="Server" CommandName="View_Details" Text="View Details"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Command>
</asp:Gridview>


For Selected-Index-Changed event, we can find controls by using gridview "SelectedRow" property as below:-

protected void grid_employee_SelectedIndexChanged(object sender,EventArgs e) 

{
Label lbl_name = (Label)grid_employee.SelectedRow .Findconrol("lbl_employee_name");
}


But for Row-Command event, we can find controls as

protected void grid_employee_RowCommand(object sender, GridViewCommandEventArgs e)

{
if(e.CommandName.ToLower.Equals("view_details".ToLower())
{
Label lbl_name = (Label)grid_employee.Rows[index] .Findconrol("lbl_employee_name");

//Here index will be Gridview row'index.
}
}


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response