如何把在ASP.NET GridView中的标头中的按钮? [英] How to put a button in a header of ASP.NET gridview?
问题描述
所有我想要做的就是把一个asp:按钮在GridView的标题中。
比方说,你有3列的网格视图 - ID,姓名,家庭。因此,而不是一个标题[ID,姓名,家庭]我希望它是[ID,名称,ASP:按钮] - 使按键都会有它的过程中的作用
All I want to do is to put an asp:button in the header of a gridview. Let's say you have a grid view with 3 columns - id, name, family. So, instead of a header [id, name, family] i want it to be [id, name, asp:button] - so the button will have it's action of course.
谢谢,
罗马。
Thanks, Roman.
推荐答案
你需要采取的方法是使所需的GridView列模板字段。当你使用模板字段,你可以坚持任何asp.net控制到GridView控件(标题,项目,页脚等)的各个主要部分。我没有测试过这一点,但在主要的它看起来像这样:
The approach you'll need to take is to make that desired GridView column a template field. When you use template fields you can stick any asp.net controls into each main section of the gridview control (header, item, footer, etc). I haven't tested this but in principal it looks like this:
<asp:GridView ID="Gridview1" runat="server">
<Columns>
<asp:BoundField DataField="yourDbColumnName" HeaderText="id" />
<asp:BoundField DataField="yourDbColumnName" HeaderText="name" />
<asp:TemplateField>
<HeaderTemplate>
<asp:Button runat="server" ID="btnFamily" CommandName="FamilyClicked" />
</HeaderTemplate>
<ItemTemplate>
<asp:Literal runat="server" ID="litFamily" Text='<%# EVAL("YourDbColumnValue") %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
要捕获你的按钮的单击事件,并用它做什么,你需要利用GridView的RowCommand事件(这里是一个起点 - 又未经测试):
To "capture" your button's click event and do something with it you need to utilize the GridView's RowCommand event (here's a starting point - again untested):
Protected Sub Gridview1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles Gridview1.RowCommand
If e.CommandName = "FamilyClicked" Then
' they clicked by grid view header's asp:button control...
Response.Write("TEST")
End If
End Sub
在这里神奇的是在你的按钮的CommandName属性的分配(在这种情况下,我将其设置为FamilyClicked但它可以是任何你想要的)。
The magic here is in the assignment of your button's CommandName property (in this case I set it to "FamilyClicked" but it can be anything you want).
下面是关于模板字段技术GridView控件使用了一些更基础 - 链接文字
Here is some more fundamentals on the Template Field technology the GridView uses - link text
希望有所帮助。
这篇关于如何把在ASP.NET GridView中的标头中的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!