Reputation: 9720
I have on ListView
<asp:ListView ID="MyListView" runat="server">
<LayoutTemplate>
<ul class="menu">
<li><a href="/"><i></i>home</a></li>
<li runat="server" />
<li class="myClass">
<a title="SomeTitle" href="example.com/default.aspx">
</a>
</li>
</ul>
<ascx:Menu runat="server" Visible="true" />
</LayoutTemplate>
<ItemTemplate>
<li>
<asp:HyperLink runat="server" NavigateUrl='<%# Eval("URL") %>'
Text='<%# Eval("Title") %>'/></li>
</ItemTemplate>
<SelectedItemTemplate>
<li class="active">
<asp:HyperLink runat="server" NavigateUrl='<%# Eval("URL") %>'
Text='<%# Eval("Title") %>' /></li>
</SelectedItemTemplate>
</asp:ListView>
on .cs file I have one string
protected string CurrentName = MyMethod();
//that return a string or null
in aspx page I want to show in <LayoutTemplate>
different structure of tags in dependecy of my CurrentName
I try like this:
<% if(this.CurrentName ==null){ %>
<ul class="menu">
<li><a href="/"><i></i>home</a></li>
<li runat="server" />
<li class="myClass">
<a title="SomeTitle" href="example.com/default.aspx">
</a>
</li>
</ul>
<ascx:Menu runat="server" Visible="true" />
<% } else { %>
<ul class="menu">
<li><a href="/"><i></i>home</a></li>
<li class="myClass">
<ascx:Menu runat="server" Visible="true" />
</li>
</ul>
<% } %>
this method not work, how use IF statements in ASPX page right?
Upvotes: 1
Views: 3037
Reputation: 56688
I would implement this with two panels which have exlusive conditions on Visible
attribute, so that only one of them is rendered at a single load:
<asp:Panel runat="server" Visible='<%# this.CurrentName == null %>'>
<ul class="menu">
<li><a href="/"><i></i>home</a></li>
<li runat="server" />
<li class="myClass">
<a title="SomeTitle" href="example.com/default.aspx">
</a>
</li>
</ul>
<ascx:Menu runat="server" Visible="true" />
</asp:Panel>
<asp:Panel runat="server" Visible='<%# this.CurrentName != null %>'>
<ul class="menu">
<li><a href="/"><i></i>home</a></li>
<li class="myClass">
<ascx:Menu runat="server" Visible="true" />
</li>
</ul>
</asp:Panel>
Upvotes: 2