user1131661
user1131661

Reputation: 229

Add a clickable image to a GridView in ASP.Net

I'm trying to add an delete image at the end of each row in a GridView. I want the user to be able to click the image to delete the row.

So I've used a HyperLinkField to create a link to another page which will delete the record:

<asp:HyperLinkField DataNavigateUrlFields="ID" 
                        DataNavigateUrlFormatString="RemoveLine.aspx?ID={0}"                             
                        Target="_blank"   />

The HyperLinkField doesn't contain an Image tag so I created a TemplateField with an Image inside.

<asp:TemplateField>
    <ItemTemplate>
        <asp:Image ID="imageRemove" runat="server" ImageUrl="~/Images/smallcross.gif"  />                                
    </ItemTemplate>                        
</asp:TemplateField>

However the HyperLinkField and Image appear in different columns and the image has no click event.

Any way of combining the two?

I'm using ASP.Net 4.0.

Thanks in advance

Upvotes: 3

Views: 28864

Answers (4)

Nudier Mena
Nudier Mena

Reputation: 3274

<asp:TemplateField>
 <ItemTemplate>
    <asp:ImageButton ID="btnDelete" runat="server"  
        ImageUrl="~/Imags/delete.png" OnClick="btnDelete_Click"
 ToolTip="Delete row" CommandName="Eliminar" CommandArgument='<%#Eval("UserId")%>'/>
 </ItemTemplate>

You can use the CommandArgument to pass the ID value of the selected row and perform the desires results

// fires when the ImageButton gets clicked
protected void GridView1_ItemCommand(object sender, DataGridCommandEventArgs e)
{
  if(e.Commandname ="Eliminar"){
  this.Eliminar(Convert.ToInt32(e.CommandArgument));

}

}

// function to delete the record
 private void Eliminar(int code)
 { 
    //custom code to delete the records
 }

Upvotes: 2

Nudier Mena
Nudier Mena

Reputation: 3274

you can wrap your image with a raw anchor tag , you get the same result.

<asp:TemplateField>

<ItemTemplate>

<a href="RemoveLine.aspx?ID={0}">      
<asp:Image ID="imageRemove" runat="server" ImageUrl="~/Images/smallcross.gif" />                                              
</a>
</ItemTemplate>

</asp:TemplateField>

Upvotes: 0

ArBR
ArBR

Reputation: 4082

Normally you need to identify which record you want to delete, you can use the CommadArgument property to identify the record's Id:

<asp:TemplateField HeaderStyle-Width="40">
    <ItemTemplate>
        <asp:ImageButton ID="ButtonDelete" runat="server"  
            ImageUrl="~/Imags/delete.png" OnClick="ButtonDelete_Click" ToolTip="Delete"
            CommandArgument='<%#Bind("UserId")%>'/>
    </ItemTemplate>
</asp:TemplateField>

protected void ButtonDelete_Click(object sender, EventArgs e)
{
    ImageButton button = sender as ImageButton;
    DeleteUserById(Convert.ToInt32(button.CommandArgument));
}

Upvotes: 5

Steve Wellens
Steve Wellens

Reputation: 20638

How about an image button?

<ItemTemplate>
        <asp:ImageButton ID="ImageButton1" runat="server" 
            ImageUrl="~/Images/smallcross.gif" onclick="ImageButton1_Click" />
</ItemTemplate>

Upvotes: 0

Related Questions