Suppress viewstate __viewstate rendering

G

Guest

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.
 
G

Guest

Just a thought
Is there way to set style display as none for hidden variable ??

vinay
 
G

Guest

I tried your suggestion but it did not work. See
http://pettysconsulting.com/test.htm. It has this defined:

<style type="text/css">
input { font-family: Verdana; display: none; }
</style>

Neither type="text" nor type="hidden" elements are shown in the web page,
but both are shown in Outlook (with Verdana font).
 
D

Dave Fancher

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
 
G

Guest

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 grabbed
into a string like you suggest because it's a bunch of ASP.NET controls that
make up a timesheet entry/report page. The timesheet entry page is also the
timesheet report page by means of polymorphism to generate the correct data
controls (eg, textbox or label) depending on whether the timesheet is
editable. This is really slick because there's only one place to make
changes when modifying what a timesheet 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
timesheet 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 said:
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
 
D

Dave Fancher

I was afraid you were going to say that ;) I'm not sure of a way to grab
the HTML before it has been rendered but since you may be able to get away
with a different type of client side solution.

On your read only view you could add a button to open a new window and
dynamically populate the contents with JavaScript. I have included some
sample code below. Basically, just wrap everything you want to include on
the page you want to e-mail in a <span> and dynamically write the contents
to the new window. If the areas you want to e-mail are non-linear you could
use the getElementsByTagName() or getElementsByName() functions instead.
You said that your client is using IE so this should work just fine. Keep
in mind that JavaScript must be enabled.

<script language="javascript">
function OpenEMailWindow() {
var newWindow = window.open("about:blank", "EMailWindow");
newWindow.document.write("<html><head><title>EMail Sample</title><link
rel=\"stylesheet\" href=\"styles\/default.css\"></head><body>");
newWindow.document.write(document.getElementById("EMailableContent").innerHTML);
newWindow.document.write("</body></html>");
}
</script>
....
<span id="EMailableContent">
All of your e-mailable contents would go here. Clicking the button below
will copy <b>everything</b> in this block to be copied to the new window.
</span>
<input type="button" value="EMail Friendly" onclick="OpenEMailWindow();">
....

HTH
--
Dave Fancher
http://davefancher.blogspot.com


Jason Pettys said:
That may be an option but it will take a lot of effort compared with FileSend > Page by E-mail. I don't know that the HTML of the page can be
grabbed
into a string like you suggest because it's a bunch of ASP.NET controls
that
make up a timesheet entry/report page. The timesheet entry page is also
the
timesheet report page by means of polymorphism to generate the correct
data
controls (eg, textbox or label) depending on whether the timesheet is
editable. This is really slick because there's only one place to make
changes when modifying what a timesheet 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
timesheet 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
 
M

Matt Berther

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>>
 
D

Dave Fancher

The code below was a wild guess (a worth trying thing). It copies the
content properly but doesn't render the html in the mail message.

Sorry!
 
S

Steven Cheng[MSFT]

Hi Jason,

I think Matt's suggestion on using the STringBuilder and HtmlTextWriter to
intercept the asp.net server control's output html is reasonable. In fact,
we can use this means to also get the HtmlForm or other control's output
html in a stringbuilder. Then, we can do some modification as we want, such
as remove all the <input type="hidden" ..> element or replace them with
other elements in it..... After that, you can just insert the modified
html string into your email body.

If there is any other questions or concerns, please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

You're welcome.

Have a good day!

Regards,

Steven Cheng
Microsoft Online Support
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top