2011-02-12 43 views
0

在PHP中可以有服務器端腳本打印回使用類似我該如何response.write一段代碼?

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

什麼是ASP當量(C#)一個格式化的代碼塊?

它似乎並不喜歡當我不把所有的字符串保留在一行,我想避免必須連接所有的東西,因爲它會殺死保持格式化的可讀性的目的。這就是我所不喜歡的。我知道爲什麼它這樣做,但我想知道如何實現你可以在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>"); 
%> 

這不是任何人會看到。我只是想爲自己建立一個扔掉的數據輸入頁面。

更新:從不知道...我剛剛意識到這不會爲我工作,因爲它會重複每個控件ID。失敗。

無論如何,以備將來參考。有沒有辦法做到我想要做的事情,只要代碼塊去?

+0

[在C#定界符字符串]的可能重複(http://stackoverflow.com/questions/3538484/heredoc-strings-in-c) – BoltClock 2011-02-12 23:34:49

回答

1

你可能會考慮使用<asp:Repeater />爲了這個目的:

<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> 

在後面的代碼,你會然後綁定此網頁加載(或者其它任何合適)的列表或數據源。最後,檢查每個項目綁定時自定義處理的ondataitembound操作。

1
<%= Some String Expression Here %>

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

第一是更好,如果你想打印一個語句。如果你有多個表達式打印內容(如果使用循環,if/else等等),則第二種效果會更好。

請記住,這被認爲是不好的做法,並且在ASP.NET Web中被壓垮了形式世界。你應該使用控件。

如果你想創建一些更復雜的佈局,重複某些事情,你應該使用數據綁定控件。對於使用GridView控件的表,使用DropDownList的選項列表以及其他用途的佈局使用ListView控件(您也可以將它用於表格)。這些控件爲每個項目使用默認格式或模板,並通過後面的代碼綁定到項目集合。所有這些都是針對ASP.NET Web窗體的。如果你使用的是ASP.NET MVC,那麼事情就會有所不同。

+0

我會到達那裏。仍在學習。我試圖做的是使用循環打印出一些錶行。我想保留html格式,但這裏的第二個例子不喜歡當我不把所有的東西放在同一行上 – Sinaesthetic 2011-02-12 23:38:34

+0

我已經更新了答案。 – Stilgar 2011-02-13 00:09:48