2

Having the following markup

    <li class="blabla">
        <asp:LinkButton ID="myBtn" runat="server" OnCommand="myBtn_Command" />
    </li>

Is there an elegant way to render it conditionally without making li item a server element (runat="server") ? Overriding Render() is not an option as well.

p.s. This is a footer content to a list, but as far as i know there is no way to control FooterTemplate visibility neither in Repeater nor in ListView.

3 Answers 3

4

You could use an approach with a Panel (or div) container so that you either hide or display 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 show button is a protected/public boolean variable on server side code, which will take the value true or false according to your condition of showing the button or not.

The other way you can do it, is make the ShowButton variable a string that will take the values 'visible' or 'hidden' accordingly and set the visibilty of the li this way. I.e.

<li class="blabla" style='visibility: <%= ShowButton %';">
        <asp:LinkButton ID="myBtn" runat="server" OnCommand="myBtn_Command" />
 </li>
Sign up to request clarification or add additional context in comments.

2 Comments

This is the same as making li item a server element plus additional html container i don't need (Panel is always rendered to div, isn't it?)
@frogbot Yes, panel is rendered to a div, and you can do that with a div as well, with the main difference being that 'ShowButton' should be a string and take the values 'hidden' or 'visible', and would pretty much do the same thing. On the other hand, you can do it setting the visiblitiy of the li much the same way. I'll edit it in my answer for you.
3

To dynamically change the visibility of a repeater's footerTemplate, you could make use of the repeater's ItemDataBound Event:

    protected void rptTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            e.Item.Visible = false;
        }
    }

Another option might be to dynamically render some javascript to hide/display your content, but this wouldn't stop the content from rendering in the first place.

Thirdly, the asp:placeholder control does not render any html of its own, so you could use that in place of the asp:Panel in Nikos Steiakakis' answer.

1 Comment

How could i miss this functionality? Thank you!!
0

Your best option is the simplest one, which is to do as you suggest and make the li a server side control by adding runat="server". That is what runat="server" is for.

In light of your comments, you might try a Placeholder control and in your server side code, optionally output your listitem and button.

2 Comments

I don't want my ViewState to grow or have an autogenerated id in the output markup.
This will make an insignificant difference to your ViewState. Really. Try it and see. If you don't want autogenerated id in your markup, upgrade to ASP.NET 4, where you can control the ClientIDs, or if this is not an option, use MVC which is totally clean and has no ViewState.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.