0

In php you can have the serverside script print back a block of formatted code by using something like

print <<<HERE
     <code>
          <somemorecode></somemorecode>
     </code>
HERE;

What is the asp equivalent (C#)??

It doesn't seem to like when I don't keep all of the string on one line and i'd like to avoid having to concatenate everything as it kind of kills the purpose of preserving the formatting for readability. Here's what I have that it isn't liking. i know why its doing it, but I'd like to know how to achieve what you can in php:

    <%
        for(int i = 20; i<21;i++){                
        Response.Write("
        <tr>
            <td class="style1">
                <asp:DropDownList ID="docNumber" runat="server" />
            </td>
            <td class="style1">
                <asp:Label ID="Label1" runat="server" Text="This would be the title of the document." /></td>
            <td class="style1">#</td>
            <td class="style1">
                <asp:DropDownList ID="supervisorName" runat="server" />
            </td>            
        </tr>");
 %>

This isn't for anything anyone will ever see. I'm just trying to build a throw away data entry page for myself.

update: nevermind... i just realized this isn't going to work for me as it will duplicate each control id. fail.

At any rate, for future reference. is there a way to do what i was trying to do as far as the code blocks go?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Sinaesthetic
  • 11,426
  • 28
  • 107
  • 176
  • possible duplicate of [Heredoc strings in C#](http://stackoverflow.com/questions/3538484/heredoc-strings-in-c) – BoltClock Feb 12 '11 at 23:34

2 Answers2

1
&lt%= Some String Expression Here %>

or

&lt% code with Response.Write("some string"); %>

the first is better if you want to print a single statement. The second is better if you have more than one expression that prints stuff (if you use loops, if/else, etc.)

Keep in mind that this is considered bad practice and is frowned upon in the ASP.NET Web Forms world. You should be using controls.

If you want to create some more complex layout that repeats something you should use databound controls. For tables use the GridView control, for list of options use DropDownList and for other things use layout use the ListView control (you can use it for tables too). These controls use default formatting or template for each item and are bound to a collection of items via code behind. All this is for ASP.NET Web Forms. If you are using ASP.NET MVC things are done differently.

Stilgar
  • 22,354
  • 14
  • 64
  • 101
  • i'll get there. still learning. what im trying to do is use a loop to print out a number of table rows. I want to keep the html formatting but the second example here doesn't like when i dont keep everything on the same line – Sinaesthetic Feb 12 '11 at 23:38
1

You might consider using an <asp:Repeater /> for this purpose:

<asp:Repeater runat="server" id="repeater1">
    <HeaderTemplate>
        <table>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>                      
            <td class="style1">
                <asp:DropDownList ID="docNumber" runat="server" /></td>
            <td class="style1">                          
                <asp:Label ID="Label1" runat="server" 
                Text="<%# Container.DataItem("DocTitle") %>"/></td>                          
            <td class="style1">#</td>
            <td class="style1">                          
                <asp:DropDownList ID="supervisorName" runat="server" /></td>                              
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

In your code behind, you would then bind this to a list or datasource on page load (or where ever appropriate). Finally check out the ondataitembound action for custom handling of each item as it's bound.

Prescott
  • 7,312
  • 5
  • 49
  • 70
  • GridView is what should be used for tables. What is more I cannot imagine a case where Repeater is better than ListView. I believe the Repeater is just a legacy control these days. – Stilgar Feb 13 '11 at 00:11