Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to create multiple actions for a gridview - say, "Approve", "Deny", and "Return".

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, this creates one column for each button.

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

like image 918
chris Avatar asked Feb 25 '11 16:02

chris


2 Answers

Have you considered using an 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>

You can then capture the commands the exact same way using OnRowCommand or do whatever you like. You have full control to make it behave how you need and not be bound by the built in functionality of using the regular predefined column types.

like image 144
Kelsey Avatar answered Sep 26 '22 04:09

Kelsey


Try to put buttons into the <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>
like image 21
Oleks Avatar answered Sep 26 '22 04:09

Oleks