CHANGE AUTOGENERATEDDELETE LINKBUTTON OF GRIDVIEW TO AN IMAGE BUTTON

Brad Pitt

Total Post:61

Points:429
Posted by  Brad Pitt
C#  Gridview 
 4358  View(s)
Ratings:
Rate this:

i have a gridview with AutoGenerateDeleteButton Property set true. Of course this property auto generates a linkbutton at the leftmost of the gridview, my question is, how can i change it to an Image Button?? i wanted my gridview to look more presentable by making the control buttons an image.

Thanks! 

  1. Simons Hood

    Post:70

    Points:494
    Re: Change AutoGeneratedDelete LinkButton of Gridview to an Image Button

    Try this 

    <Columns>
       <asp:CommandField ButtonType="Image" DeleteImageUrl="~/Images/DeleteImage.png"
          ShowDeleteButton="true"/>
    </Columns>

    And set AutoGenerateDeleteButton="false"

      Modified On Apr-07-2018 05:09:51 AM
  1. Jayden Bell

    Post:110

    Points:774
    Re: Change AutoGeneratedDelete LinkButton of Gridview to an Image Button

    You can create a TemplateField and use  autogeneratecolumns="false". 

    Here's an example of a GridView: 

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false">
        <Columns>
            <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="Link" runat="server" Text="click" OnClick="link_Click"/>
            </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="field1" HeaderText="My Column 1" />
            <asp:BoundField DataField="field2" HeaderText="My Column 2" />
         </Columns>
    </asp:GridView>

    Where field1 and field2 are headers from your DataTable 

    And to access the row within the event handler: 

    protected void link_Click(object sender, EventArgs e)
    {
        int rowindex = ((GridViewRow)((Control)sender).NamingContainer).RowIndex;
        //do something with rowindex etc
    }

      Modified On Apr-07-2018 05:09:23 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!