Re: Suppress viewstate __viewstate rendering

From: Matt Berther (mberther_at_hotmail.com)
Date: 01/15/05


Date: Fri, 14 Jan 2005 23:36:41 -0800

Hello Jason,

If you put an asp:Button on the page, and contain the content you wish to
have rendered in a panel, something like this will do the trick (Im using
this exact technique to email portions of a site):

StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
using (HtmlTextWriter writer = new Html32TextWriter(sw))
{
    theContentPanel.RenderControl(writer);
}

// at this point sb.ToString() contains a string of everything in that panel.
You're free to send this off via email or whatever.

I hope this is what you were after.

--
Matt Berther
http://www.mattberther.com
> That may be an option but it will take a lot of effort compared with
> File >
> 
Send>> Page by E-mail.  I don't know that the HTML of the page can be
Send>> grabbed
Send>> 
> into a string like you suggest because it's a bunch of ASP.NET
> controls that make up a time*** entry/report page.  The time***
> entry page is also the time*** report page by means of polymorphism
> to generate the correct  data controls (eg, textbox or label)
> depending on whether the time*** is editable.  This is really slick
> because there's only one place to make changes when modifying what a
> time*** is.
> 
> Is there a way to grab the HTML that an arbitrary ASP.NET page will
> generate?  I tried HtmlForm.InnerText and .InnerHtml but that throws
> an HttpException: "Cannot get inner content of FormActiveJobs because
> the contents are not literal."  I think that's a short description of
> what I'm trying to explain in the above paragraph.
> 
> I think to do what you suggest I'd have to write a brand new view on
> the time*** data that can just dump HTML as a string.  But that
> introduces duplication that will have ongoing maintenance costs.  I'd
> rather find a way to hack the __VIEWSTATE so it doesn't show up.
> 
> If you know of a way to grab the HTML an arbitrary ASP.NET page will
> generate into a string or stream I'd like to know of it.
> 
> Jason
> 
> "Dave Fancher" wrote:
> 
>> Have you considered a server side solution that generates an e-mail
>> wth the BodyFormat property of your MailMessage set to
>> MailFormat.Html?  Obviously I am not familiar with your project but
>> typically when someone wants to e-mail a page, it's a report or flyer
>> of some sort which is dynamically generated.
>> 
>> If this is the case, you may want to switch to a method that
>> generates the HTML and puts it in a PlaceHolder for the page output
>> and can also be set as the Body property of your MailMessage.
>> 
>> HTH
>> 
>> --
>> Dave Fancher
>> http://davefancher.blogspot.com
>> "Jason Pettys" <pettys@nospam.nospam> wrote in message
>> news:355837CC-7A79-4163-83D8-B62A9D1EEEC2@microsoft.com...
>> 
>>> I have an ASP.NET page that my client wants to e-mail.  So in IE we
>>> choose File > Send > Page by E-mail, which opens up either Outlook
>>> or Outlook Express with the HTML page content as the message body.
>>> 
>>> EXCEPT one nasty detail is that in the e-mail client, <input
>>> type="hidden">
>>> elements are rendered as text boxes, which the __VIEWSTATE is one
>>> of, and
>>> so
>>> the viewstate is displayed in a textbox at the top of the e-mail.
>>> I've figured out some overrides and stuff to ensure that the textbox
>>> is empty, so the HTML renders
>>> 
>>> <input type="hidden" name="__VIEWSTATE" value="">
>>> 
>>> but this still displays an empty TextBox.
>>> 
>>> Is there any way to prevent the HTML element from being written at
>>> all?  I found an interesting explanation of why it's hard at this
>>> address: http://scottonwriting.net/sowblog/posts/1608.aspx
>>> 
>>> But couldn't translate that into a solution.
>>> 
>>> --
>>> Jason
>>> www.pettysconsulting.com