简体   繁体   English

asp.net gridview:如何在一列中有多个按钮字段?

[英]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". 我需要为gridview创建多个动作 - 比如“Approve”,“Deny”和“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? 有没有办法拥有相同的功能,但将它们组合成一个列?

Have you considered using an TemplateField ? 你考虑过使用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. 然后,您可以使用OnRowCommand以完全相同的方式捕获命令,或者执行您喜欢的任何操作。 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. 您可以完全控制它使其按您需要的方式运行,而不受使用常规预定义列类型的内置功能的约束。

Try to put buttons into the <asp:TemplateField> instead: 尝试将按钮放入<asp:TemplateField>

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton CommandName="Approve" Text="Approve" /> 
        <asp:LinkButton CommandName="Deny" Text="Deny /> 
        <asp:LinkButton CommandName="Return" Text="Deny" />
    </ItemTemplate>
</asp:TemplateField>

The solution is not to use ButtonField elements. 解决方案不是使用ButtonField元素。

To accomplish what you want you will need to create a column as a TemplateField and define buttons as regular ASP.NET <asp:Button id="myButton" /> within TemplateField 's ItemTemplate or EditItemTemplate as appropriate for your UI. 要完成您想要的任务,您需要创建一个列作为TemplateField并在TemplateFieldItemTemplateEditItemTemplate中将<asp:Button id="myButton" />定义为常规ASP.NET <asp:Button id="myButton" /> ,以适合您的UI。

You can handle Click events in the GridView_OnItemCommand() handler, where you can check e.CommandName to figure out exactly which button caused the event. 您可以在GridView_OnItemCommand()处理程序中处理Click事件,您可以在其中检查e.CommandName以确切地确定导致事件的按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM