We have answer of your question!

100% solved queries, no empty question

Question: repeater how to avoid blank html


0

Advertisement


We currently have an application which we have a repeater

<asp:Repeater ID="rptOfficials" runat="server">
  <ItemTemplate>
    <tr>
      <td>
        <p class="officials">
          <%#Eval("OffPosition") %> <%#Eval("FullName") %>
        </p>
        <p class="officials">
          <%#Eval("Phone") %>
        </p>
      </td>
    </tr>
  </ItemTemplate>
</asp:Repeater>

The Phone field is not always supplied, when not there a blank paragraph tag is generated, we're thinking of changing

        <p class="officials">
          <%#Eval("Phone") %>
       </p>

to either:

    <p class="officials">
      <asp:label id="lblPhone" runat="server"/>
    </p>

or:

    <p class="officials">
      <%#GetPhone("OffId") %>
    </p>

and then have either the label return the phone number with the paragraph tags wrapped around it, or have the function return it. Is there a better way to get around this?

Question author Paritosh | Source

Answer


1


Advertisement


It depends how your Binding model is composed but I suggest adding the logic in your property (Phone) on your page your (view)model (yes you can and should use ViewModels in web forms as well).

That way you keep logic outside the view.

public string Phone {
   get { 
       return this.phone ?? string.Empty;
   }
}

That way your logic stays nicely inside your view model and your view just takes care of binding it.

If you don't want to generate the

-tag, you can add a boolean property to your model.

public bool DisplayPhone{
    get {
        return this.Phone == string.Empty;
    }
}

You then need to bind it to the visible property as Halcyon mentioned. (note you need a runat="server" for this)

<p runat="server" class="officials" visible='<% #Eval("DisplayPhone") %>'>
    <% #Eval("Phone") %>
</p>
Answer author Ronald

Advertisement


Tickanswer.com is providing the only single recommended solution of the question repeater how to avoid blank html under the categories i.e asp.net , . Our team of experts filter the best solution for you.

Related Search Queries:

You may also add your answer!