简体   繁体   中英

how use If Statement in aspx page

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM