How to disable ViewState for a .aspx page? - asp.net

I need to completely disable ViewState for a .aspx page inside my web application. I have gone through different blogs and what I understand is that we must set <%# Page EnableViewState="false" ...%>, but this is not working.
Is this enough to disable ViewState for all the controls inside the page? Or should I make any additional modifications? If so, please specify them. I don't want the ViewState enabled for even a single control inside the .aspx page

Disabling View State
Machine Level -
Disabling view state at machine level in machine.config, will disable ViewState of all the applications on the web server.
<Machine.config>
<system.web>
<pages enableViewState="false" />
</system.web>
</Machine.config>
Application Level -
You can disable ViewState for all pages in /web.config file.
<configuration>
<system.web>
<pages enableViewState="false" />
</system.web>
</configuration>
Page Level -
Disabling view state for a specific aspx file at the top.
<%# Page Language="C#" .. EnableViewState="false" .. %>
Control Level - You can disable ViewState for a specific control.
<asp:TextBox EnableViewState="false" ID="Name"
runat="server"></asp:TextBox>

I think the quotes should be:
EnableViewState="false"
Apart from that, if you are still seeing the hidden fields then they are used by ASP.Net. You may see:
Page.EnableViewState Property
Even if EnableViewState is false, the page might contain a hidden view
state field that is used by ASP.NET to detect a postback.

If you truly don't need postback, you can remove the form element from your page, this will remove the viewstate entirely.

Related

ASP.NET 3.5 ClientIDs refined

I've created a simple ASP.NET 4.0 application to see how rendered client ids will vary if I change controlRenderingCompatibilityVersion in web.config and ClientIDMode attribute of the control.
Now I've set <pages controlRenderingCompatibilityVersion="3.5"/> and <asp:Label runat="server" ID="Message" ClientIDMode="AutoID" /> and expect to find in the generated markup asp.net-3.5-style ClientID (something like id='ctl00_Message'),
but I see this <span id="Message">Hello world!</span>.
Why does not ASP.NET render it as a 3.5-style id?
If the span is on it's own, then there is no reason to change the ID.
It depends if the control is inside another control. If the span above is inside a Repeater, DataList etc, then the Id will change.

disabling viewstate of whole page

Is it possible to make the viewstate false of whole page including all controls at a time.I mean I don't want to set enableviewstate="false" for all controls..In the page directive of the aspx page I have made enableviewstate="false" but still viewstate of all the controls of the is enabled..
And what the EnableViewState="False"actually works within Page directive.
Have you tried setting enableViewState to false in web.config? Like this:
<pages enableViewState="false" />
In ASP.Net 4.0 one of the new features is more control over the ViewState of a Page and its Controls.
In ASP.Net 3.5 and earlier, the ViewState of ChildControls is ignored, if the ViewState for the Page is set explicitly. So if you set EnableViewState="false" for a UserControl, it still will use ViewState, if the Page.EnableViewState is set to true.
ASP.Net 4.0 introduces a new ViewStateMode-Property with 3 Values: Enabled, Disabled and Inherit
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" ViewStateMode="Disabled" Inherits="_Default" %>
So if you set ViewStateMode to Inherit for child controls, they will inherit the page's behaviour. If you set it to Enabled or Disabled, they will do as you want them to.
See for more info: http://www.dotnetcurry.com/ShowArticle.aspx?ID=478&AspxAutoDetectCookieSupport=1
Even though it does not directly answer your question, why your EnableViewState value in the Page gets ignored, but it might show you how to do it in ASP.Net 4.0 or where to look for the problem. Maybe you set EnableViewState="true" on a higher level, like the MasterPage?
This is (a somewhat dated) but still very relevant article about ViewState and how it works.
Here is another good article on MSDN.
I suggest you read and understand both before deciding that ViewState is not off.

Change UserControl template at runtime

Is it possible to change the ascx file used by a usercontrol at runtime?
For example in my page i have
<ctl:SampleControl runat="server" />
in the web.config I have
<controls>
<add tagPrefix="ctl" tagName="SampleControl" src="~/UserControls/SampleControl.ascx" />
</controls>
At runtime I would like to be able to change the ascx to another path, it would still be inheriting from the same usercontrol codebehind, it would just be a different template.
Probably the best thing to do here is to not encode the filename of the "default" .ascx file in your web.config. It will make your life harder. Always do that determination at runtime, like this for instance:
In your .aspx file:
<asp:PlaceHolder runat="server" ID="samplePH" />
In the code-behind:
string file = "~/UserControls/SampleControl.ascx";
if (condition)
file = "~/UserControls/OtherControl.ascx";
UserControl uc = (UserControl)LoadControl(file); // from System.Web.UI.TemplateControl.
samplePH.Controls.Clear();
samplePH.Controls.Add(uc);
But, be aware that in order for post-backs to work correctly, you need to instantiate the same control that was loaded on the last request, early in the page lifecycle -- typically the Init stage. This will ensure that viewstate is correctly parsed. Then, further down in your event handler, PreRender, etc. lifecycle steps, you can use the above code to load the UserControl for the current request.
If you really do need to encode a default page setting in a configuration file (for cases where end-users may want to change it), consider doing it in an app.config rather than buried away in a <controls> section of a web.config.
Documentation for the TemplateControl.LoadControl(string) method:
http://msdn.microsoft.com/en-us/library/system.web.ui.templatecontrol.loadcontrol.aspx

Cannot see new asp.net user controls

I am feeling like an idiot right now, but I cannot for the life of me figure out why I cannot seem to call user controls from within my asp.net webpage. I'm still learning asp.net but I can't find any information from searching on google.
I'm trying to load a specific control on the page when the user presses a linkbutton. So I created an empty user control via the right click menu:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
In other words, I have not touched any part of the created user control. Yet attempting to create this web control and add it to the form seems to not work, as it claims that the WebUserControl class does not exist (I have no other controls in my project):
UserControl blah = new WebUserControls();
produces a "The type or namespace is invalid". Why can none of my asp.net webform pages get the control into scope?
The new control must be added to the site's Web.config file.
<configuration>
<system.web>
<pages>
<controls>
<add tagPrefix="my"
tagName="WebUserControl"
src="~/WebUserControl.ascx"/>
</controls>
</pages>
</system.web>
</configuration>
Use this to place the new control in an .aspx page.
<my:WebUserControl runat="server" ID="MyWebUserControl" />
In addition to registering them one-by-one in web.config, you can also use a <%# Register %> directive in the source of the markup files that reference the control.
Or, you can move your controls into a DLL and include them all by referencing the assembly from your web.config (takes some extra work, though).

Do you disable viewstate by default when developing asp.net webform?

we know that view state is easily get abused, but asp.net webform are heavily depending on this feature.
I want to know whether you disable viewstate by default, and ONLY add it wehn it's needed. Or you take the visual studio default, which actually enables viewstate by default.
I tend to take the default of having it on when doing web.forms.
But when writing my own user controls I disable it until I need it, or I find something doesn't work. I also monitor the size of the view state and when/if it gets too big I look at what the page is doing and change what I'm doing on the page. I.e. bind to data objects containing only the data that is needed and no more... stuff like that.
As others have said, unless we are talking about a fairly static page, I tend to leave the ViewState on while developing, and begin selectively disabling it when the page works. That way, it's one less thing to worry about upfront.
You may find this interesting, in ASP.NET 4.0, we will have more efficient control over disabling the ViewState: http://www.asp.net/learn/whitepapers/aspnet40/#_Toc223325478
Personnaly, no. Eventually, to have it disabled will be the behaviour that you will want but most of the time, no. Also, I would do a manual ViewState optimization pass and disabled controls' ViewState that wouldn't need it if really necessary. It's better not to have the ViewState to worry about while you are in heavy development imo. I'm not saying here that you shouldn't care about the ViewState at all but to put EnableViewState aside until you feel the need to lighten the trips to the server.
Turn off the ViewState by default by using a <page> element in the web.config. Using EnableViewState="true" in the #Page directive will no longer work once you disable the ViewState in the web.config. If you decide later that you need the ViewState for a specific page, you can turn it back on for just that page using a <location> element.
<configuration>
<system.web>
<pages enableViewState="false" />
</system.web>
<location path="MyFolder/MyPage.aspx">
<system.web>
<pages enableViewState="true" />
</system.web>
</location>
<location path="Site.master">
<system.web>
<pages enableViewState="true" />
</system.web>
</location>
</configuration>
You need to do the same for any master pages that your ViewState enabled page uses.
I go one step further, I've created a class that inherits from the Page class and override 2 functions
protected override void SavePageStateToPersistenceMedium(object viewState)
{
}
protected override object LoadPageStateFromPersistenceMedium()
{
return null;
}
Then have my page that requires the viewstate to be disabled inherit from this.
Works very well.

Resources