Hi all,

I hav an application running in ASP.Net using VB. I hav two textboxes which carries user's input and a button. On Click of this button, the values in the textboxes will be added and be displayed in a label in a new window. I hav declared a variable to carry the result and display it in the label. Now, in the new window i hav a button to email the page content and the result label which is in a panel. I'm using the below code to email the new window content.

Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim SB As New StringBuilder()
Dim sw As New StringWriter(SB)
Dim htmlTW As New HtmlTextWriter(sw)
Label1.RenderControl(htmlTW)

Dim printlabel As String = SB.ToString()

'create the mail message
Dim mail As New MailMessage()

'set the addresses
mail.From = New MailAddress("")
mail.To.Add("")
mail.IsBodyHtml = True

'set the content
mail.Subject = "Test Message"
mail.Body = printlabel

'send the message
Dim smtp As New SmtpClient("")
smtp.Send(mail)
Response.Write("Message Sent")
End Sub

Query: When I email the new window, i can only get the result label displayed. I want the result label to be displayed with the panel. Can anyone help me on this.

Dani AI

Generated

A quick, practical add-on to the thread: ’s mail code captures only the single label because that’s the exact control being rendered. ’s suggestion to render a larger container is the right direction, but there are a few important server-side details to check so the panel (and everything inside it) actually shows up in the email.

First, make sure the page you’re rendering is the server-side page (not a client-side popup or DOM-only page). If the new window’s content or the label text is set by client-side JavaScript, RenderControl won’t see those changes. The control you want to email must exist and have its content set on the server (for example in Page_Load or on a server postback) before you capture its output. Also verify the container you render is a server control (runat="server").

If you get the “must be placed inside a form with runat=server” error when rendering a control manually, add this empty override to the page code-behind to allow rendering outside the normal lifecycle:

Public Overrides Sub VerifyRenderingInServerForm(control As System.Web.UI.Control)
    ' Intentionally left blank so controls can be rendered manually
End Sub

If rendering the panel directly is awkward, two alternatives: render a small user control (.ascx) that contains just the result block and render that control, or capture the whole page output by executing the page (Server.Execute) and use the resulting HTML. Both approaches avoid grabbing only the single label while keeping server-side values intact.

Finally, remember email client constraints: inline your CSS or use absolute URLs for images/resources (relative paths will break), remove or avoid JavaScript in the mail HTML, and test the resulting message in common clients (Gmail/Outlook) to verify layout. Also ensure your SMTP host/credentials and error handling are correct so failed sends are visible during testing.

Hi.

Try exchanging Label1.RenderControl(htmlTW) with Page.RenderControl(htmlTW) .
This will give you the entire page including all controls.

Or, you can put the code inside a <div> tag like this:

<div id="divPanel" runat="server">
<your code>
</div>

And then use divPanel.RenderControl(htmlTW) .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.