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

Discussion in 'ASP .Net' started by =?Utf-8?B?SmFtZXMgUGFnZQ==?=, Jul 4, 2006.

  1. 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??
     
    =?Utf-8?B?SmFtZXMgUGFnZQ==?=, Jul 4, 2006
    #1
    1. Advertising

  2. Hi,

    James Page wrote:
    > 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
    --
    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
     
    Laurent Bugnion, Jul 5, 2006
    #2
    1. Advertising

  3. Hi,

    Laurent Bugnion wrote:
    > 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
     
    Laurent Bugnion, Jul 5, 2006
    #3
  4. Thanks for the reply Laurent

    Do you have an example in VB.net??

    "Laurent Bugnion" wrote:

    > Hi,
    >
    > Laurent Bugnion wrote:
    > > 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
    >
     
    =?Utf-8?B?SmFtZXMgUGFnZQ==?=, Jul 5, 2006
    #4
  5. Hi James,

    James Page wrote:
    > 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
    --
    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
     
    Laurent Bugnion, Jul 5, 2006
    #5
  6. =?Utf-8?B?SmFtZXMgUGFnZQ==?=

    zambodi

    Joined:
    Jan 17, 2008
    Messages:
    1
    Switch from vb.net to c#

    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
     
    zambodi, Apr 14, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. msnews.microsoft.com
    Replies:
    2
    Views:
    1,543
    msnews.microsoft.com
    Apr 28, 2005
  2. =?Utf-8?B?TGVlIENoYXBtYW4=?=

    How to Switch HtmlTextWriter via browserCaps in web.config

    =?Utf-8?B?TGVlIENoYXBtYW4=?=, Aug 18, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,231
    Steven Cheng[MSFT]
    Aug 24, 2005
  3. refv8
    Replies:
    2
    Views:
    27,184
    ittower
    Oct 13, 2006
  4. msnews.microsoft.com

    Do we always have to use the HtmlTextWriter to create the control?

    msnews.microsoft.com, Apr 27, 2005, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    136
    MasterGaurav
    May 5, 2005
  5. Martin
    Replies:
    1
    Views:
    197
    Martin
    Aug 31, 2006
Loading...

Share This Page