By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,636 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

Getting the Html text for a control...

P: n/a
In our application, I would like to send out HTML mail. TO do so, I must do something like this:

Mail.Body = <raw HTMLTEXT String>

Hence, I would like to know how to get the HTML text for Web Controls.

You'll notice below in lines 33-37 that that code does correctlt return the HTML for a textbox. How do I make it generic so it returns the HTML for any control. Hopefully, if the control is a container control, it will return the HTML for ALL nested controls, too.

When I hit line 40, I get the error: Control 'Button1' of type 'Button' must be placed inside a form tag with runat=server.

Thanks for any comments that may be offered.

Chad

1 Public Class WebForm1
2 Inherits System.Web.UI.Page
3
4 #Region " Web Form Designer Generated Code "
5
6 'This call is required by the Web Form Designer.
7 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
8
9 End Sub
10 Protected WithEvents Button1 As System.Web.UI.WebControls.Button
11
12 'NOTE: The following placeholder declaration is required by the Web Form Designer.
13 'Do not delete or move it.
14 Private designerPlaceholderDeclaration As System.Object
15
16 Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
17 'CODEGEN: This method call is required by the Web Form Designer
18 'Do not modify it using the code editor.
19 InitializeComponent()
20 End Sub
21
22 #End Region
23
24 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
25 'Put user code to initialize the page here
26
27 Dim s As String = GetHtmlForControl(Button1)
28
29 End Sub
30 Public Function GetHtmlForControl(ByVal Control As System.Web.UI.Control) As String
31
32
33 'Dim Tbx As New TextBox
34 'Tbx.Text = "Hello World!"
35 'Dim Writer As HtmlTextWriter = New HtmlTextWriter(New System.IO.StringWriter)
36 'Tbx.RenderControl(Writer)
37 'Dim s As String = Writer.InnerWriter.ToString()
38
39 Dim Writer As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(New System.IO.StringWriter)
40 Control.RenderControl(Writer)
41 Dim Htmltext As String = Writer.InnerWriter.ToString()
42 Return Htmltext
43
44 End Function
45 End Class



Server Error in '/WebApplication1' Application.
--------------------------------------------------------------------------------

Control 'Button1' of type 'Button' must be placed inside a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'Button1' of type 'Button' must be placed inside a form tag with runat=server.

Source Error:

Line 38:
Line 39: Dim Writer As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(New System.IO.StringWriter)
Line 40: Control.RenderControl(Writer)
Line 41: Dim Htmltext As String = Writer.InnerWriter.ToString()
Line 42: Return Htmltext

Source File: c:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.v b Line: 40
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Looks like as long as the control is created a run time, I dont have a problem.

Thanks anyways.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

Dim B As New Button

Dim T As New Table

Dim TableRow As New TableRow

T.Rows.Add(TableRow)

Dim C As New TableCell

TableRow.Cells.Add(C)

C.Controls.Add(B)

Dim HTML As String = GetHtmlForControl(T)

End Sub

Public Function GetHtmlForControl(ByVal Control As System.Web.UI.Control) As String

'Dim Tbx As New TextBox

'Tbx.Text = "Hello World!"

'Dim Writer As HtmlTextWriter = New HtmlTextWriter(New System.IO.StringWriter)

'Tbx.RenderControl(Writer)

'Dim s As String = Writer.InnerWriter.ToString()

Dim Writer As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(New System.IO.StringWriter)

Control.RenderControl(Writer)

Dim Htmltext As String = Writer.InnerWriter.ToString()

Return Htmltext

End Function

"Chad" <ch**************@unisys.com> wrote in message news:dg**********@trsvr.tr.unisys.com...
In our application, I would like to send out HTML mail. TO do so, I must do something like this:

Mail.Body = <raw HTMLTEXT String>

Hence, I would like to know how to get the HTML text for Web Controls.

You'll notice below in lines 33-37 that that code does correctlt return the HTML for a textbox. How do I make it generic so it returns the HTML for any control. Hopefully, if the control is a container control, it will return the HTML for ALL nested controls, too.

When I hit line 40, I get the error: Control 'Button1' of type 'Button' must be placed inside a form tag with runat=server.

Thanks for any comments that may be offered.

Chad

1 Public Class WebForm1
2 Inherits System.Web.UI.Page
3
4 #Region " Web Form Designer Generated Code "
5
6 'This call is required by the Web Form Designer.
7 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
8
9 End Sub
10 Protected WithEvents Button1 As System.Web.UI.WebControls.Button
11
12 'NOTE: The following placeholder declaration is required by the Web Form Designer.
13 'Do not delete or move it.
14 Private designerPlaceholderDeclaration As System.Object
15
16 Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
17 'CODEGEN: This method call is required by the Web Form Designer
18 'Do not modify it using the code editor.
19 InitializeComponent()
20 End Sub
21
22 #End Region
23
24 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
25 'Put user code to initialize the page here
26
27 Dim s As String = GetHtmlForControl(Button1)
28
29 End Sub
30 Public Function GetHtmlForControl(ByVal Control As System.Web.UI.Control) As String
31
32
33 'Dim Tbx As New TextBox
34 'Tbx.Text = "Hello World!"
35 'Dim Writer As HtmlTextWriter = New HtmlTextWriter(New System.IO.StringWriter)
36 'Tbx.RenderControl(Writer)
37 'Dim s As String = Writer.InnerWriter.ToString()
38
39 Dim Writer As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(New System.IO.StringWriter)
40 Control.RenderControl(Writer)
41 Dim Htmltext As String = Writer.InnerWriter.ToString()
42 Return Htmltext
43
44 End Function
45 End Class

Server Error in '/WebApplication1' Application.
------------------------------------------------------------------------------

Control 'Button1' of type 'Button' must be placed inside a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'Button1' of type 'Button' must be placed inside a form tag with runat=server.

Source Error:

Line 38:
Line 39: Dim Writer As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(New System.IO.StringWriter)
Line 40: Control.RenderControl(Writer)
Line 41: Dim Htmltext As String = Writer.InnerWriter.ToString()
Line 42: Return Htmltext

Source File: c:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.v b Line: 40
Nov 19 '05 #2

P: n/a
You could do something like this (it's same idea, just for a different use):

http://staff.develop.com/ballen/blog...2-8095a848d821

-Brock
DevelopMentor
http://staff.develop.com/ballen
In our application, I would like to send out HTML mail. TO do so, I
must do something like this:

Mail.Body = <raw HTMLTEXT String>

Hence, I would like to know how to get the HTML text for Web Controls.

You'll notice below in lines 33-37 that that code does correctlt
return the HTML for a textbox. How do I make it generic so it returns
the HTML for any control. Hopefully, if the control is a container
control, it will return the HTML for ALL nested controls, too.

When I hit line 40, I get the error: Control 'Button1' of type
'Button' must be placed inside a form tag with runat=server.

Thanks for any comments that may be offered.

Chad

1 Public Class WebForm1
2 Inherits System.Web.UI.Page
3
4 #Region " Web Form Designer Generated Code "
5
6 'This call is required by the Web Form Designer.
7 <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
8
9 End Sub
10 Protected WithEvents Button1 As
System.Web.UI.WebControls.Button
11
12 'NOTE: The following placeholder declaration is required by
the Web Form Designer.
13 'Do not delete or move it.
14 Private designerPlaceholderDeclaration As System.Object
15
16 Private Sub Page Init(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Init
17 'CODEGEN: This method call is required by the Web Form
Designer
18 'Do not modify it using the code editor.
19 InitializeComponent()
20 End Sub
21
22 #End Region
23
24 Private Sub Page Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
25 'Put user code to initialize the page here
26
27 Dim s As String = GetHtmlForControl(Button1)
28
29 End Sub
30 Public Function GetHtmlForControl(ByVal Control As
System.Web.UI.Control) As String
31
32
33 'Dim Tbx As New TextBox
34 'Tbx.Text = "Hello World!"
35 'Dim Writer As HtmlTextWriter = New HtmlTextWriter(New
System.IO.StringWriter)
36 'Tbx.RenderControl(Writer)
37 'Dim s As String = Writer.InnerWriter.ToString()
38
39 Dim Writer As System.Web.UI.HtmlTextWriter = New
System.Web.UI.HtmlTextWriter(New System.IO.StringWriter)
40 Control.RenderControl(Writer)
41 Dim Htmltext As String = Writer.InnerWriter.ToString()
42 Return Htmltext
43
44 End Function
45 End Class
Server Error in '/WebApplication1' Application.
----------------------------------------------------------------------
--- -------

Control 'Button1' of type 'Button' must be placed inside a form tag
with runat=server. Description: An unhandled exception occurred during
the execution of the current web request. Please review the stack
trace for more information about the error and where it originated in
the code.

Exception Details: System.Web.HttpException: Control 'Button1' of type
'Button' must be placed inside a form tag with runat=server.

Source Error:

Line 38:
Line 39: Dim Writer As System.Web.UI.HtmlTextWriter = New
System.Web.UI.HtmlTextWriter(New System.IO.StringWriter)
Line 40: Control.RenderControl(Writer)
Line 41: Dim Htmltext As String =
Writer.InnerWriter.ToString()
Line 42: Return Htmltext
Source File: c:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.v b
Line: 40

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.