John Chapman

[ASP.NET] Multiple Command (Select, Delete, etc.) Fields in a GridView

Share on TwitterShare on LinkedInShare on TumblrSubmit to StumbleUponSave on DeliciousDigg ThisSubmit to reddit

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 only provides you with the ability to have a single select, single delete, and single edit command. You cannot have multiples of each. I did, however, discover the . Using the you can assign a command name to each button (which can be a regular button, image button, or link button) and execute the appropriate code accordingly.

In your add an OnRowCommand call:

<asp:GridView ID="gvData" OnRowCommand="gvData_RowCommand" ...>

Add your (you can do this with the WYSIWYG in Visual Studio as well):

<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.

Source

Share on TwitterShare on LinkedInShare on TumblrSubmit to StumbleUponSave on DeliciousDigg ThisSubmit to reddit

John Chapman

Hello, I'm John Chapman. I am a SharePoint developer living in Denver, Colorado. I develop solutions using ASP.NET, C#, jQuery, SQL, SharePoint, etc, and I thrive on the challenge of writing code to overcome the impossible, annoying, or otherwise difficult obstacles.

More Posts - Website - Twitter

Leave a comment

John Chapman