Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i want to add an imagebutton on each header column with text so that user can click to book that item.
Posted

You can add a template column and add link button to it. If you want an image you can use image button. If you want both image and text, then you can embed images tag inside the link button.
Bind the command argument property with the ID of the item and handle the RowCommand event of the grid view.

Grid view:

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="Book">
<ItemTemplate>

<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%# Bind("ID") %>' CommandName="bookitem">
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/Filter.gif" /> Book this item</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Code behind:

Protected Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "bookitem" Then
'' add code here
'' you can use e.CommandArgument to get the CommandArgument
End If
End Sub
 
Share this answer
 
Comments
abhinavstar921 10-Sep-14 2:49am    
if the bind id is dynamic then how to use???
Abdul Samad KP 10-Sep-14 5:50am    
Sorry, I didn't get
What did you mean by dynamic?
You can use HeaderTemplate and add any control to it...

Just like ItemTemplate used in second solution..

read more about HeaderTemplate Here


C#
<asp:gridview id="GridView1" 
        width="250" 
        runat="server">

        <columns>
          <asp:templatefield>
            
            <headertemplate>
              <asp:ImageButton ID="btn1" runat="server" ImageUrl="img1.gif" OnClick="btn1_Click"/>
            </headertemplate>
          </asp:templatefield>                      
        </columns>

      </asp:gridview>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900