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