Add Page Directives Programmatically?

  • Thread starter Thread starter xeroxero
  • Start date Start date
X

xeroxero

I would like to set the following Page directives in my C# code-behind
for ASP.NET 2.0 Web Application Project and also in a ASP.NET 2.0 Web
Site. What event should I hook and what should the code look like?

MaintainScrollPositionOnPostback
ViewStateEncryptionMode
LinePragmas
Title


Thanks.
 
xeroxero,

The first two are simple:

Page.MaintainScrollPositionOnPostBack = true;
Page.Title = "hello";

The page ViewStateEncryptionMode property complains with an exception if you
try to set it manually: "The 'ViewStateEncryptionMode' property can only be
set in the page directive or in the <pages> configuration section"

Probably for security reasons, if a page has been configured to require
encrypted viewstate... well... allowing the code to change this would be
incorrect (given that the declaritive security requirements should override
requirements expressed in code). So... you're out of luck there.

However there is the "RegisterRequiresViewStateEncryption()"... which will
force the veiwstate to be encrypted. This must be done in prerender or
before.

As to LinePramas... I think you're out of luck. LinePragmas is more of an
instruction to the ASP.NET compiler (the one that produces pure C#/VB.NET
code that is then compiled by .net). By the time your code is running...
it's already way too late (code has been generated and compiled).

Regards,

Rob MacFadyen
 
Thank for Rob's informative suggestions.

Hi Xeroxero,

I think Rob has answered all your questions completely.

Just some add on to the ViewState encryption, if you want to use
Page.RegisterRequiresViewStateEncryption to programmatically set ViewState
be encrypted, you need to ensure the "viewStateEncryptionMode" is set as
Auto(default setting) in @page directive or web.config. You can get some
more detailed description about ViewState encryption in ASP.NET 2.0 and its
related settings in the following msdn article:

#How To: Configure MachineKey in ASP.NET 2.0
http://msdn.microsoft.com/library/en-us/dnpag2/html/paght000007.asp?frame=tr
ue

As for the "LinePramas", I'm afraid Rob is right, this is a pure compile
time specfic setting and at runtime, we haven't any means to configure
it(even if we have such a property, it's too late at runtime to control
compilation behavior).

If there is any other information you wonder, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello Xeroxero,

Have you got any further ideas on this question or does our reply help you
a little? If there is anything else we can help, please feel free to post
here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top