MoonLight MoonLight - 1 year ago 97
ASP.NET (C#) Question

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 id="Content" style="padding: 10px 10px;">
<ul style="margin:0px;padding:0px 10px 10px 10px;">
title1 : ---
title 2 :
<asp:Label ID="lblOredr_Id" runat="server" Text="---" ForeColor="Red"></asp:Label>
<br />
<p style="text-align: center;color:#fd00dc;">
bla bla</p>

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

string s = MainDiv.InnerHtml.ToString();


Cannot get inner content of MainDiv because the contents are not

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

Answer Source

You can't invoke InnerHtml on a div unless its contents are literal (i.e. 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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download