HtmlTextWriter - send control via e-mail asp.net 2.0

G

Guest

I can't seem to get HtmlTextWriter / renderControl to work for sending a
control (html) in an e-mail messsage (VB.NET).
Keeps telling me that control needs to be in a form tag with runat server
(which it is)
Tried googling but only good for C# and classic asp.
Has anyone seen or has a solution??
 
L

Laurent Bugnion

Hi,

James said:
I can't seem to get HtmlTextWriter / renderControl to work for sending a
control (html) in an e-mail messsage (VB.NET).
Keeps telling me that control needs to be in a form tag with runat server
(which it is)
Tried googling but only good for C# and classic asp.
Has anyone seen or has a solution??

I am struggling with that kind of problem too, in my case I attempt to
render the whole page to an external (static) html file.

Apparently, when the control is rendered, a few basic rules are checked.
For example, before the control is rendered, its FORM control must be
rendered. Also (and it's what I am trying to solve now), the FORM
control can be rendered only once. I am not sure if there is a method to
call to render controls to another writer without having these rules
throwing exceptions.

In your case, try to render the FORM control before you render the
child, maybe it'll work.

HTH,
Laurent
 
L

Laurent Bugnion

Hi,

Laurent said:
Hi,

I am struggling with that kind of problem too, in my case I attempt to
render the whole page to an external (static) html file.

Apparently, when the control is rendered, a few basic rules are checked.
For example, before the control is rendered, its FORM control must be
rendered. Also (and it's what I am trying to solve now), the FORM
control can be rendered only once. I am not sure if there is a method to
call to render controls to another writer without having these rules
throwing exceptions.

In your case, try to render the FORM control before you render the
child, maybe it'll work.

HTH,
Laurent

Update: I managed to render my page to the external file by overriding
the Page's Render method:

protected override void Render( HtmlTextWriter writer )
{
if ( Request.Form != null
&& Request.Form[ bnProduce.ClientID ] != null )
{
this.RenderPage();
writer.WriteLine( "It worked" );
writer.WriteLine( "<br /><a href=\""
+ this.Request.RawUrl + "\">back</a>" );
}
else
{
base.Render( writer );
}
}

The "trick" in my case was to comply to the rules, and writing the FORM
control only once in the same postback

Needs some cleaning, but the basic concept works.

HTH,
Laurent
 
G

Guest

Thanks for the reply Laurent

Do you have an example in VB.net??

Laurent Bugnion said:
Hi,

Laurent said:
Hi,

I am struggling with that kind of problem too, in my case I attempt to
render the whole page to an external (static) html file.

Apparently, when the control is rendered, a few basic rules are checked.
For example, before the control is rendered, its FORM control must be
rendered. Also (and it's what I am trying to solve now), the FORM
control can be rendered only once. I am not sure if there is a method to
call to render controls to another writer without having these rules
throwing exceptions.

In your case, try to render the FORM control before you render the
child, maybe it'll work.

HTH,
Laurent

Update: I managed to render my page to the external file by overriding
the Page's Render method:

protected override void Render( HtmlTextWriter writer )
{
if ( Request.Form != null
&& Request.Form[ bnProduce.ClientID ] != null )
{
this.RenderPage();
writer.WriteLine( "It worked" );
writer.WriteLine( "<br /><a href=\""
+ this.Request.RawUrl + "\">back</a>" );
}
else
{
base.Render( writer );
}
}

The "trick" in my case was to comply to the rules, and writing the FORM
control only once in the same postback

Needs some cleaning, but the basic concept works.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
 
L

Laurent Bugnion

Hi James,

James said:
Thanks for the reply Laurent

Do you have an example in VB.net??

Sorry, I don't do VB.NET anymore, but you should be able to translate
quite easily, because the methods and properties bear the same names.
What I do is:

- Check if the Request.Form property exists (it is null if this is a
GET, i.e. if the page is loaded for the first time), and if the
Request.Form property exists, I check if the ID of the button is present
in the request. If it is, then it means that the button was clicked.

The reason why I do this is that I need to check which button was
clicked before the page is rendered. If I use normal events, they will
be executed too later, after Page_Load is called.

- If the button ID is found, i.e. if the button was clicked, I use my
RenderPage method instead of using the base class' method. I just
noticed that I didn't copy the RenderPage method in my previous post,
see below.

- To avoid rendering the Form control twice (which causes the error), I
write simple code to the HtmlTextWriter (just a link) instead of calling
base.Render().

- If the button was not clicked, I call the Render method of the base
class, which will simply and normally render all the controls on the
page to the Response object.

The RenderPage method looks like this:

private void RenderPage()
{
HtmlTextWriter writer = null;

try
{
writer
= new HtmlTextWriter(
new StreamWriter(
"c:\\temp\\static.html", false ) );

foreach ( Control child in this.Controls )
{
child.RenderControl( writer );
}
}
catch ( Exception ex )
{
throw ex;
}
finally
{
if ( writer != null )
{
writer.Close();
}
}
}

This method is quite simple too: I create a new HtmlTextWriter which
will write to a Static file located at c:\temp\static.html (in the real
code I use properties to do that, but it doesn't matter).

Then I loop through all the controls on the page, and instead of
rendering them to the Response object, I render them to the static file
using the Writer I just created. The try/catch/finally is just to make
sure that the Writer is closed even if there is an error.

HTH,
Laurent
 
Joined
Jan 17, 2008
Messages
1
Reaction score
0
Switch from vb.net to c#

Laurent Bugnion said:
Sorry, I don't do VB.NET anymore...

Bugnion,

I'm just curious as to your reasons from switching from VB.Net to C#? I'm trying to support a position for switching our team over to C# and would appreciate and respect your input.

Thanks,

Zam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top