Guo Hong Lim Guo Hong Lim - 3 months ago 15
ASP.NET (C#) Question

Server Tag not well formed with ASP:Button

I have read through the various topics and found none that assisted me in my situation.

Based on whether it is 1 or 0 for my sold variable, i want to be able to show or hide a button accordingly.

I did the following but I am getting a server tag not well formed error and despite changing the quotes ' and " (which i knew wouldn't help), it still doesn't solve the error.

<asp:Button ID="btnMarkAsSold" OnClick="btnWantSell" runat="server" Text="Mark as sold" class="btn btn-warning btn-block higher bold" <%#(Eval("Sold").ToString() == "1" ? "style='display:none'" : String.Empty) %> />
<asp:Button ID="btnSold" runat="server" Text="SOLD" class="btn btn-danger btn-block higher bold" <%#(Eval("Sold").ToString() == "0" ? "style='display:none'" : String.Empty) %> />

Answer

Tested and below is working

The problem is style=display:none. Create a class like hidden and update your button as:

<asp:Button ID="btnSold" runat="server" Text="SOLD" class='btn btn-danger btn-block higher bold <%#(Eval("Sold").ToString() == "0" ? "hidden" : String.Empty) %>'  />

<asp:Button ID="btnMarkAsSold" OnClick="btnWantSell" runat="server" Text="Mark as sold" class='btn btn-warning btn-block higher bold <%#(Eval("Sold").ToString() == "1" ? "hidden" : String.Empty) %>' />

CSS

hidden{ display:none;}

Visible property

To use Visible property of button as suggested in @Mahmood's answer

<asp:Button ID="btnSold" runat="server" Text="SOLD" class="btn btn-danger btn-block higher bold" Visible="<%# Eval("Sold").ToString() == "0" ? false : true %>" />