简体   繁体   中英

how can get innerhtml of a server side element with c# (with another server-side controls inside)

how can get innerhtml of a server side element with c# (with another server-side controls inside)?
or what is the best way for designing email body with visual studio 2010?
i have some codes in c# for sending emails...(supporting html body)
so i am looking for a way for scape of hardcoding html body!
and so made a server side div in design mode:

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

but i can n't get innterHtml of this div for my email body by below code:

 string s = MainDiv.InnerHtml.ToString();  

ERROR:

Cannot get inner content of MainDiv because the contents are not literal.

also i have a server side table inside MainDiv And i want to add some data dynamically to this table.
with this situation what can i do for getting html for email body?
thanks in advance

You can't invoke InnerHtml on a div unless its contents are literal (ie there aren't server controls contained inside it). But you can force it to render to a string by doing something like this:

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

string s = sb.ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM