[ASP.NET] Multiple Command (Select, Delete, etc.) Fields in a GridView
I recently had the requirement to provide multiple select-type commands on an ASP.NET GridView that each resulted in a different action. Using an
In your
<asp:GridView ID="gvData" OnRowCommand="gvData_RowCommand" ...>
Add your
<asp:GridView ID="gvData" OnRowCommand="gvData_RowCommand" ...> <Columns> <asp:ButtonField ButtonType="Link" CommandName="Select" Text="Select" /> <asp:ButtonField ButtonType="Link" CommandName="Email" Text="Email" /> <asp:ButtonField ButtonType="Button" CommandName="Print" Text="Print" /> ... </Columns> </asp:GridView>
In your code-behind, you simply need to use an “If” or a “Switch” to determine which command to execute:
protected void gvData_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "Select")
{
}
else if(e.CommandName == "Email")
{
}
else if(e.CommandName == "Print")
{
}
}That’s it. It’s rather simple and it helped me, so I wanted to share. Hopefully you find it useful.
Leave a comment
Follow John Chapman
SharePoint StackExchange
- http://t.co/d2YzH8q1 - #sharepoint - [SharePoint 2010] Specifying Which Server a Custom Timer Job Will Run On
- Always have your stuff when you need it with @Dropbox. 2GB account is free! http://t.co/kczsnniq
- http://t.co/iLWV2Kwp - #sharepoint - [SharePoint 2010] Web Analytics: Monitors the health of the Report Consolidator component
Recent Posts
- [SharePoint 2010] Specifying Which Server a Custom Timer Job Will Run On
- [SharePoint 2010] Web Analytics: Monitors the health of the Report Consolidator component
- [SharePoint 2010 / SQL Server 2008] Query the SharePoint Object Model from a .NET SQL Server CLR Function
- [SharePoint 2010] Debugging a Custom SharePoint Timer Job
- [SharePoint 2010] Set Access Request Email for All SharePoint Sites
