asp.net gridview: How can I have multiple button fields in one column?

I need to create several actions for the gridview — for example, Approve, Deny, and Revert.

I can do this by creating a button field for each action:

<asp:ButtonField ButtonType="Link" CommandName="Approve" Text="Approve" /> <asp:ButtonField ButtonType="Link" CommandName="Deny" Text="Deny /> <asp:ButtonField ButtonType="Link" CommandName="Return" Text="Deny" /> 

However, a single column is created for each button.

Is there a way to have the same functionality, but combine them into one column?

+7
webforms gridview
source share
3 answers

Do you consider using TemplateField ? Here is an example:

 <asp:GridView ID="grdTest" runat="server"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="btnApprove" runat="server" CommandName="Approve" Text="Approve" /> <asp:LinkButton ID="btnDeny" runat="server" CommandName="Deny" Text="Deny" /> <asp:LinkButton ID="btnReturn" runat="server" CommandName="Return" Text="Return" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> 

Then you can capture commands in exactly the same way using OnRowCommand or do whatever. You have full control over how it behaves the way you need it, and should not be bound by the built-in functions of using standard predefined column types.

+8
source share

Try putting buttons in <asp:TemplateField> instead:

 <asp:TemplateField> <ItemTemplate> <asp:LinkButton CommandName="Approve" Text="Approve" /> <asp:LinkButton CommandName="Deny" Text="Deny /> <asp:LinkButton CommandName="Return" Text="Deny" /> </ItemTemplate> </asp:TemplateField> 
+3
source share

The solution is not to use ButtonField elements.

To accomplish what you need, you need to create a column as a TemplateField and define the buttons as normal ASP.NET <asp:Button id="myButton" /> within the TemplateField ItemTemplate or EditItemTemplate , as appropriate for your user interface.

You can handle Click events in the GridView_OnItemCommand() handler, where you can check e.CommandName to find out which button triggered the event.

+3
source share

All Articles