Show only 32 results in C# [closed]

You’re concatenating your desired output to a temporary variable then outputting that.

Change it from desc += "<BR... to desc = "<BR....

Other issues:

  • Your i1 variable has no use outside of the for loop, so move the declaration inline: for(int i1 = 1; i1 <= 32; i1++) {
  • "<BR>" is not keeping with present-day HTML convention. We use lowercase tags, and use <br /> if you want compatibility with XHTML and XML.
  • As others have pointed out, your if statement’s expression will always evaluate to false.
  • String mutation operations (concatenation, Substring, Remove, etc) are generally expensive (as they involved the allocation of a new string) and should be avoided in high-performance code. In your case the call to desc.Remove(0,6) is unnecessary because you can output the string directly.
  • It looks like you’re writing HTML inside a render function block (the <% %>). Your code might be easier to read if you keep all HTML as literals instead, like so:

    <% for(int i = 1; i <= 32; i++) { %>
    PCDC_Description<%: i %><br />
    <% } %>
    
  • Assuming you’re using ASP.NET 4.0 or later, you should use <%: instead of <%= or <% Response.Write( because it automatically performs HTML-encoding of strings, this is essential to avoid a variety of injection attacks such as XSS.

Browse More Popular Posts

Leave a Comment