disabling viewstate of whole page - asp.net

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.

Related

How to disable/override naming container's ID generation of Content page's control id's

We have an existing ASP.Net web application which we want to convert into using masterpages.
In the process of doing this, I found that the HTML id's generated for the HTML elements are prefixed with the ContentPlaceHolder's id. And this is what can be expected when we set the ContentPlaceHolder's clientidmode=static.
Now since we have a lot of existing client side scripts that make use of the id's, this part breaks when we use masterpages, and it is quite a big job to run through all our javascript to make sure we call the javascript using Control.ClientID, as a lot of it is hardcoded.
Is there a way to disable the prefixing? I can succeed doing this, if I create every control setting its ClientIdMode=static, but then again I would prefer settings this once, to ensure that all controls are have their ClientIdMode=static. Is that possible? Or is it possible to override the NamingContainer of the ContentPlaceHolder?
The platform is .Net 4.0
(After fixing the above problem with ClientIdMode=static in the web.config as described in the answer below), I have bumped into the problem that the "name" attribute is automatically generated, and is not set to whatever it was before I introduced masterpages. This gives me a problem with my existing server code, which has many Request.Form[]. Any idea what best practice is here to solve this problem?
Thanks
Jihad
You can have ClientIDMode at Page Level:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ClientIDMode="Static" %>
MasterPage Level:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" ClientIDMode="Static" %>
and Web.Config level (making all pages inherit this behavior):
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<pages clientIDMode="Static"></pages>
</system.web>
In case you don't target .Net framework 4, and the clientIDMode enum is not available, you can simply override the control class. Here is an example with HtmlGenericControl but can be done with any other control:
public class NoNamingContainerControl : HtmlGenericControl
{
public NoNamingContainerControl(string tag) : base(tag) { }
public override string ClientID
{
get
{
return this.ID;
}
}
}
This code should work on any .Net framework versions, although in .net 4 you should probably use the clientIDMode

How to disable ViewState for a .aspx page?

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.

Dynamically registering controls in .NET

Can anyone tell me if I can dynamically set the file name when registering a user control please, for example:
<%# Register src="[file name]" tagname="WebUserControl" tagprefix="uc1" %>
No, I don't believe you can. What you can do is register all of the possible controls that you might use on the page, either in the page directive or the web.config.
EDIT
What you can do, if this helps, is to add the controls dynamically in code-behind using the LoadControl method. This way, you can create instances of whatever user controls you want without worrying about registering them in the page directive or web.config. Thanks #Gabriel for pointing this out.

How to use outputcache in a usercontrol, with control properties

I have a UserControl, which should only change based on 2 URL parameters.
The problem is, it has a public property, which is used in the calling pages, so it throws a NullReferenceException on my property.
Any ideas?
I think I've figured this one out, it seems to be quite tricky which is due to my lack of comprehensive understanding of how output cache works I suspect.
You can't cache the UserControl if it has variable properties that dicatate it's content. You need to put a cache control in the Content page that holds the control. Then add the cache to the content page:
<%# OutputCache Duration="120" VaryByControl="JobList" %>
Where the vary by control is the ID of the control you wish to cache. Then specify a property for that vary by control:
<%# OutputCache Duration="120" VaryByControl="JobList.LoggedInUserID" %>
This seems to work for me!
Check the VaryBy options,
try to take a look to this articles:
http://msdn.microsoft.com/en-us/library/hdxfb6cy%28v=vs.71%29.aspx
https://web.archive.org/web/20211020113508/https://www.4guysfromrolla.com/articles/022802-1.aspx
http://weblogs.asp.net/stefansedich/archive/2008/03/17/output-cache-with-usercontrol-on-masterpage-and-multiple-varybycustom.aspx

ASP.NET #Register vs. #Reference

I'm working with referencing user controls on my ASPX page and I'm wondering what the difference is between these two page directives.
#Reference
#Register
#Register is primarily used for registering tag prefixes to declaratively use controls within a page.
<%# Register tagprefix="my" namespace="MyNamespace" %>
<my:CustomControl runat=server />
#Reference is primarily used to refer to a page or user control (by file name or virtual path) to programatically refer to members of the page or control.
<%# Reference Control="MyControl.ascx" %>
<% MyControl ctrl = (MyControl) Page.LoadControl("MyControl.ascx");
ctrl.CustomProperty = "..."; //REFERENCE directive is needed to access property
%>
#Register is the more commonly used directive. You use this when you want to use a user control in your aspx or ascx page declaratively. #Register associates the control with a specific prefix and you can then use it in your markup.
#Reference only tells ASP.NET to compile the other control when your aspx or ascx page is compiled. That makes sure it is available at run-time and can be added to your control hierarchy programmatically. This is less common since dynamically changing user controls at runtime is not comon.
Here's a good blog post about it.
http://weblogs.asp.net/johnkatsiotis/archive/2008/08/13/the-reference-directive.aspx

Resources