单击gridview中的编辑链接时,删除链接将消失

时间:2013-04-11 12:44:03

标签: asp.net gridview asplinkbutton

我有一个gridview,我有showitbutton属性false,showdeletebutton属性为true.Edit是一个链接按钮,点击它时会触发行命令事件。

现在,我的问题是在gridview中,当我点击编辑链接时,删除链接就会消失。 我写了

<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>

我可以得到任何帮助吗?

2 个答案:

答案 0 :(得分:1)

您可以在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>

然后只需删除CommandField

答案 1 :(得分:0)

替换以下代码

<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" />

使用此代码。

 <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>