abcd abcd - 21 days ago 6
C# Question

How to show button in gridview if a column empty

In my application i have only one editable column and if that editable column is empty i need to show add link button but it displays edit link button.How can i display add as the link button if that particular editable column in gridview is empty

<asp:TemplateField>
<itemtemplate>
<asp:Button Visible='<%# string.IsNullOrEmpty() %>' runat="server" Text="Edit" ID="Edit" CommandName="Edit" />
<asp:Button Visible='<%# !string.IsNullOrEmpty() %>' runat="server" Text="Add" ID="Add" CommandName="Edit" />
</itemtemplate>
</asp:TemplateField>


i tried this but it is working but unable to edit

Answer

The easiest way is to add a TemplateField to the GridView with 2 buttons that have a Visibility based on a column value.

<asp:TemplateField>
    <itemtemplate>
         <asp:Button Visible='<%# string.IsNullOrEmpty(Eval("editableField").ToString()) %>' runat="server" Text="Edit" ID="Edit" CommandName="Edit" />
         <asp:Button Visible='<%# !string.IsNullOrEmpty(Eval("editableField").ToString()) %>' runat="server" Text="Add" ID="Add" CommandName="Edit" />
    </itemtemplate>
</asp:TemplateField>
Comments