2011-07-22 22 views
7

如何用c#獲得服務器端元素的innerhtml(與另一個服務器端控件裏面)?
或者用visual studio 2010設計電子郵件正文的最佳方式是什麼?
我在c#中有一些代碼用於發送電子郵件...(支持html body)
所以我正在尋找硬編碼html body的方法!
等都作了設計模式的服務器端的div:如何用c#獲取服務器端元素的innerhtml(與另一個服務器端控件裏面)

<div runat="server" id="MainDiv" style="direction: rtl; font: 12px tahoma,arial,sans-serif; width: 700px; 
     background-color: #f5f5ff; border: 2px solid #003366;"> 
     <div id="Header" style="width: 690px; background-color: #637eb0; height: 40px; margin: 5px auto; 
      position: relative;"> 
      <img src="Images/logo.png" alt="logo" title="soscharge" style="width: 200px; position: absolute; 
       top: 4px; left: 10px;" /> 
      <span style="color: #69fbfd; font: bold 13px tahoma,arial,sans-serif; position: absolute; 
       top: 11px; right: 10px;">hi ...</span> 
     </div> 
     <div id="Content" style="padding: 10px 10px;"> 
      <ul style="margin:0px;padding:0px 10px 10px 10px;"> 
       <li> 
       title1 : --- 
       </li> 
       <li> 
        title 2 : 
        <asp:Label ID="lblOredr_Id" runat="server" Text="---" ForeColor="Red"></asp:Label> 
       </li> 
      </ul> 
      <br /> 
      <p style="text-align: center;color:#fd00dc;"> 
       bla bla</p> 
     </div> 
    </div> 

,但我不能用下面的代碼獲得這個div我的電子郵件的主體innterHtml:

string s = MainDiv.InnerHtml.ToString(); 

ERROR:

因爲內容不是 文字,所以無法獲得MainDiv的內容。

此外,我有MainDiv內的服務器端表我想要動態地添加一些數據到這張表。
這種情況我可以做些什麼來獲得電子郵件正文的HTML?
在此先感謝

回答

18

除非其內容是文字(即其中沒有服務器控件),否則您不能在div上調用InnerHtml。但是你可以做這樣的事情迫使它呈現爲一個字符串:

var sb = new StringBuilder(); 
mainDiv.RenderControl(new HtmlTextWriter(new StringWriter(sb))); 

string s = sb.ToString(); 
+0

感謝答案... – MoonLight

+0

@Nathan皮特曼:很好的答案..偉大的人...! – Soundar

+2

但仍然?如何獲得該控件的'innerHTML'?作爲反對刪除字符串中的文本來獲得'innerHTML',有沒有辦法做到這一點? – Pierre