Question

I am having a gridview and I have showeditbutton property false, showdeletebutton property true.Edit is a link button and a row command event is fired on clicking it.

Now,my problem is that in gridview, when I click on edit link, delete link gets disappear. I have written

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" PageSize="5" OnRowCommand="gv_RowCommand"
                        AllowPaging="true" DataKeyNames="ID" CssClass="mGrid"  BackColor="White" BorderColor="Silver" BorderStyle="Double"
                        BorderWidth="1px" CellPadding="4" >

         <RowStyle BackColor="White" Width="150%" ForeColor="#003399" />
            <Columns>
                <asp:TemplateField HeaderText="ID" Visible="false" >
                    <ItemTemplate>
                                    <asp:Label ID="lblID"  Text='<%#Bind("ID") %>' runat="server"></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField  HeaderText="Version">
                    <ItemTemplate>
                                    <asp:Label ID="lblversion" Text='<%#Bind("version") %>' runat="server"></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Image ID">
                    <ItemTemplate>
                                    <asp:Label ID="lblimageid" runat="server" Text='<%#Bind("image_id") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Getty ID">
                    <ItemTemplate>
                                    <asp:Label ID="lblgettyid" runat="server" Text='<%#Bind("getty_id") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Pool Letter">
                    <ItemTemplate>
                                    <asp:Label ID="lblpoolletter" runat="server" Text='<%#Bind("pool_letter") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="To Use">
                    <ItemTemplate>
                                    <asp:Label ID="lbltouse" runat="server" Text='<%#Bind("to_use") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Clue">
                    <ItemTemplate>
                                    <asp:Label ID="lblclue" runat="server" Text='<%#Bind("clue") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Range">
                    <ItemTemplate>
                                    <asp:Label ID="lblrange" runat="server" Text='<%#Bind("range") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="last_updated">
                    <ItemTemplate>
                                    <asp:Label ID="lbllastupdated" runat="server" Text='<%#Bind("last_updated") %>'></asp:Label>
                    </ItemTemplate>
                                        </asp:TemplateField>

                <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                                    <asp:Label ID="lblstatus" runat="server" Text='<%#Bind("status") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField  HeaderText="First">
                    <ItemTemplate>
                                    <asp:Label ID="lblfirst" runat="server" Text='<%#Bind("first") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Middle">
                    <ItemTemplate>
                                    <asp:Label ID="lblmiddle" runat="server" Text='<%#Bind("middle") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Last">
                    <ItemTemplate>
                                    <asp:Label ID="lbllast" runat="server" Text='<%#Bind("last") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>
                 <asp:TemplateField >
                    <ItemTemplate>
                                    <asp:LinkButton ID="lnkbtnedit" Text="Edit" style="color:#003399;" CommandName="Edit" runat="server"></asp:LinkButton>
                    </ItemTemplate>

                </asp:TemplateField>


                 <asp:CommandField  ShowDeleteButton="true" CausesValidation="false" />

            </Columns>
              <PagerStyle BackColor="#006699" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
                        <AlternatingRowStyle BackColor="#CCFFFF" />
                        <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                        <HeaderStyle  BackColor="#006699" Font-Bold="True" ForeColor="White"  />
    </asp:GridView>

can I get any help?

Was it helpful?

Solution

You can include the delete button in your ItemTemplate:

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton ID="lnkbtnedit" Text="Edit" style="color:#003399;" CommandName="Edit" runat="server"></asp:LinkButton>
        <asp:LinkButton ID="lnkbtndelete" Text="Delete" style="color:#003399;" CommandName="Delete" runat="server"></asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

And then just remove the CommandField.

OTHER TIPS

Replace your below code

<asp:TemplateField >
      <ItemTemplate>
         <asp:LinkButton ID="lnkbtnedit" Text="Edit" style="color:#003399;"
           CommandName="Edit" runat="server"></asp:LinkButton>
      </ItemTemplate>

</asp:TemplateField>


<asp:CommandField  ShowDeleteButton="true" CausesValidation="false" />

with this code.

 <asp:TemplateField  ItemStyle-HorizontalAlign="Center"  ItemStyle-VerticalAlign="Top"
    HeaderStyle-Width="10%">
     <ItemTemplate>
         <asp:LinkButton ID="lnkbtnedit" runat="server" CommandName="Edit" Text="Edit" />
     </ItemTemplate>

     <EditItemTemplate>                               
       <asp:LinkButton ID="btndelete" runat="server" CommandName="Delete" Text="Delete"     
          CommandArgument="<%#((GridViewRow)Container).RowIndex%>"/>

     </EditItemTemplate>             

 </asp:TemplateField>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top