The approach you need to take is to make the template field the desired GridView column. When you use the template fields, you can embed any asp.net controls in each main section of the gridview control (header, element, footer, etc.). I have not tested this, but basically 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>
To โcaptureโ a button click event and do something with it, you need to use the GridView RowCommand event (here the starting point is again unchecked):
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 asp:button control... Response.Write("TEST") End If End Sub
The magic here is to assign the CommandName property of your button (in this case, I set it to "FamilyClicked", but that may be all you want).
Here are some more basics of Template Field technology that GridView uses - link text
Hope this helps.
Steve flook
source share