Conditional rendering in ASP.NET
Having the following markup
<li class="blabla">
<asp:LinkButton ID="myBtn" runat="server" OnCommand="myBtn_Command" />
</li>
Is there an elegant way to do this conditionally without doing li
the server element ( runat="server"
)? Overriding Render()
is not an option either.
ps This is the content of the footer for the list, but as far as I know there is no way to control the visibility of the FooterTemplate in either repeaters or ListView.
a source to share
To dynamically change the visibility of the repeater footerTemplate, you can use the Relay ItemDataBound
Event:
protected void rptTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
e.Item.Visible = false;
}
}
Another option could be to dynamically render some javascript to hide / show your content, but that won't stop the content from rendering in the first place.
Third, the control asp:placeholder
does not render any native html, so you can use that instead of the answer asp:Panel
in Nikos Stejakakis' answer.
a source to share
You can use the Panel (or div) container approach so that you either hide or show the container accordingly.
i.e.
<asp:Panel ID="panelContainer" runat="server" Visible='<%= ShowButton %>'>
<li class="blabla">
<asp:LinkButton ID="myBtn" runat="server" OnCommand="myBtn_Command" />
</li>
</asp:Panel>
where the show button is a secure / public boolean on the server side which will be true or false according to your button display condition or not.
Another way you can do this is to make the ShowButton variable a string that will be "visible" or "hidden" respectively, and set the visibility of the li that way. I.e.
<li class="blabla" style='visibility: <%= ShowButton %';">
<asp:LinkButton ID="myBtn" runat="server" OnCommand="myBtn_Command" />
</li>
a source to share
The best option is the simplest one, which should do as you suggest, and whether to do server side control by adding runat = "server". To do this, use runat = "server".
In light of your comments, you can try using the Placeholder control and in your server side code, it is not necessary to render your list and button.
a source to share