2011-03-06 49 views
0
public class BlogComment 
{ 
    public int ID; 
    public int UserID; 
    public string Username; 
    public string Comment; 
    public DateTime Date; 
    public int VotesUp; 
    public int VotesDown; 

    public Panel GetCommentPanel() 
    { 
     Panel WrapPanel = new Panel(); 
     WrapPanel.CssClass = "user-comment-wrapper"; 

     Panel VotePanel = new Panel(); 
     VotePanel.CssClass = "rfloat"; 
     HyperLink UpVote = new HyperLink(); 
     HyperLink DownVote = new HyperLink(); 
     UpVote.CssClass = "s vote-box vote-up"; 
     DownVote.CssClass = "s vote-box vote-down"; 
     UpVote.NavigateUrl="#"; 
     DownVote.NavigateUrl = "#"; 
     VotePanel.Controls.Add(UpVote); 
     VotePanel.Controls.Add(DownVote); 
     WrapPanel.Controls.Add(VotePanel); 

     Panel UserTextPanel = new Panel(); 
     UserTextPanel.CssClass = "user-comment-txt"; 
     Literal UserText = new Literal(); 
     UserText.Text = this.Comment; 
     UserTextPanel.Controls.Add(UserText); 

     return WrapPanel; 
    } 

試圖生成以下HTML:覺得我做錯了生成HTML

<div class="user-comment-wrapper"> 
    <div style="float:right"> 
     <a class="s vote-box vote-up" href="#"></a> 
     <a class="s vote-box vote-down" href="#"></a> 
    </div> 
    <div class="user-comment-txt"> 
     Object comes with instantiated Department with empty atributes Object comes with instantiated Department with empty atributes Object comes with instantiated Department with empty atributes.Department with empty atributes Object comes with instantiated Department with empty atributes Object comes with instantiated Department with empty atributes.  
    </div> 
    <div class="comment-info-wrapper"> 
     <div style="float:left"> 
      <strong>Posted by <a href="#">Tom</a></strong> 
     </div> 
     <div style="float:right"> 
      <strong><abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr></strong> 
     </div> 
     <div class="clear"></div> 
    </div>    
</div> 

我的意思是它的工作原理,但我不禁覺得這個設計很爛。

+0

除了編寫自己的博客引擎外,還有其他的風格屬性(比如編寫自己的博客引擎 - 那麼多),而不是通過CSS控制UI? – kd7 2011-03-06 22:58:47

+0

@ kd7,只是因爲它需要的唯一樣式屬性是float:left或float:right這是我設計的唯一異常。 – 2011-03-06 22:59:41

+0

你爲什麼覺得這很糟糕? – 2011-03-06 23:04:58

回答

5

只需在ASPX頁面中編寫普通的老式HTML。

<div class="user-comment-wrapper"> 
    <div class="voting"> 
     <a class="s vote-box vote-up" href="#"></a> 
     <a class="s vote-box vote-down" href="#"></a> 
    </div> 
    <div class="user-comment-txt"> 
     <%: GetCommentContent() %> 
    </div> 
    <div class="comment-info-wrapper"> 
     <div class="author-info"> 
      <strong>Posted by 
       <a href="#"> <%: GetCommentAuthor() %></a> 
      </strong> 
     </div> 
     <div class="comment-info"> 
      <strong> 
       <abbr class="timeago" title="<%: GetShortCommentTime() %>"> 
       <%: GetFriendlyCommentTime() %> 
       </abbr> 
      </strong> 
     </div> 
     <div class="clear"></div> 
    </div>    
</div> 

請不要添加額外的類。使用ASP.NET 4 <%:..%>

.user-comment-wrapper .voting { float: right; } 
.comment-info-wrapper .author-info { float: left; } 
.comment-info-wrapper .comment-info { float: right; } 

你也是注入的內容(或它可以是一個通常的<%=...%>,但要確保你的HTML轉義):然後你就可以添加CSS。

我沒有看到任何理由手動創建那些絕對不可讀的服務器端控件來呈現HTML。

+0

謝謝,這可以在中繼器嗎?我以前從未使用過中繼器 – 2011-03-07 00:59:22

+0

是的,它可以。您只需要改變將內容注入HTML的方式。但是,再次,我沒有看到使用Repeater的理由:) – 2011-03-07 03:24:01