Yet again I need help, this time it's with the viewstate and the caching of files; I completely want to disable them. As I know the trafic will increase when i disable the cache, I want to do some "damage control" disabling the viewstate. My current code
generates this huge peace of viewstate code, even though I've rendered everything with EnableViewState="false".
Why are you using a ScriptManager? It registers script for the Microsoft Ajax Library with the page and enables client script to use the type system extensions and to support features such as partial-page rendering and Web-service calls. Do you reallt need
it?
You certainly do by setting EnableViewState to false. You cannot turn off the ControlState or get rid of the hidden field though.
If you set EnableViewState and EnableSessionState to false and minimize the number of server controls used to speed up the HTML rendering you should fine.
So if I understand your reply correctly, it would reduce the size of the information stored in the ViewState, if I used generic controls over the asp controls? For example
ImageButton ib = new ImageButton();
HyperLink hl = new HyperLink();
HtmlGenericControls div = new HtmlGenericControl("div");
Please remember to post your code.
If this post answered your question or solved your problem, please Mark it as Answer.
oh okay. Could i ask of you to create a small example of how to speed up the following peace of code? I'm not sure exactly how to do it without using server controls.
Default.aspx:
<form id="form" runat="server">
<asp:PlaceHolder EnableViewState="false" ID="myplaceholder" runat="server">
</asp:PlaceHolder>
</form>
Default.aspx.cs:
DirectoryInfo di = new DirectoryInfo("C:/somepath/");
foreach (FileInfo fi in di.GetFiles("*.jpg", SearchOptions.TopdirectoryOnly))
{
HyperLink hl = new HyperLink();
hl.ImageUrl = di.Name + "/" + fi.Name;
hl.NavigateUrl = di.Name + "/" + fi.Name;
myplaceholder.Controls.Add(hl);
}
Please remember to post your code.
If this post answered your question or solved your problem, please Mark it as Answer.
The only way I can think of in this example is to set the EnableViewState and EnableSessionState properties of the page to false. There is no need to for example remove the PlaceHolder control as it just renders as a simple DIV.
But if you for example have a page with a GridView control with a lot of columns and data in it, the rendered HTML can get rather heavy. That's what I meant when I suggested fever server controls. In this case, you don't have to worry about the rendering
of any controls.
Trolderik
Member
645 Points
355 Posts
Disable viewstate and caching
Apr 10, 2012 02:34 PM|LINK
Hi everyone,
Yet again I need help, this time it's with the viewstate and the caching of files; I completely want to disable them. As I know the trafic will increase when i disable the cache, I want to do some "damage control" disabling the viewstate. My current code generates this huge peace of viewstate code, even though I've rendered everything with EnableViewState="false".
Here's my code, what am I doing wrong?
Default.aspx <%@ Page Title="" EnableViewState="false" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeFile="Default.aspx" Inherits="Photo_Default" %> <asp:Content ID="ContentHead" ContentPlaceHolderID="headinc" Runat="Server"> <link href="/Photo/Default.css" rel="Stylesheet" type="text/css" /> </asp:Content> <asp:Content ID="ContentBody" ContentPlaceHolderID="ContentPlaceHolderBody" Runat="server"> <asp:ScriptManager EnableViewState="false" EnablePartialRendering="false" ID="ScriptManagerPhoto" runat="server" /> <asp:PlaceHolder EnableViewState="false" ID="photolist" runat="server> </asp:PlaceHolder> </asp:Content> Default.aspx.cs private void displayFolderContent(DirectoryInfo di) { foreach (FileInfo fi in di.GetFiles("*.jpg", SearchOption.TopDirectoryOnly)) { HtmlGenericControl div = new HtmlGenericControl("div"); div.Attributes.Add("class", "listview"); ImageButton ib = new ImageButton(); ib.AlternateText = fi.Name; ib.CausesValidation = false; ib.ImageUrl = "/Photo/" + di.Name + "/" + fi.name; ib.OnClientClick = "window.location.replace('/Photographies/" + di.Name + "/" + fi.Name + "'); return false;"; ib.ViewStateMode = ViewStateMode.Disabled; div.Controls.Add(ib); photolist.Controls.Add(div); } }Also, if there's any surgestion to optimize the code even further, I am open for surgestions.
Best Regards
If this post answered your question or solved your problem, please Mark it as Answer.
mm10
Contributor
6409 Points
1184 Posts
Re: Disable viewstate and caching
Apr 10, 2012 02:40 PM|LINK
Why are you using a ScriptManager? It registers script for the Microsoft Ajax Library with the page and enables client script to use the type system extensions and to support features such as partial-page rendering and Web-service calls. Do you reallt need it?
Trolderik
Member
645 Points
355 Posts
Re: Disable viewstate and caching
Apr 10, 2012 03:29 PM|LINK
I'm using it for this:
protected void Page_Load(object sender, EventArgs e) { string folder = Page.RouteData.Values["folder"] as string; string file = Page.RouteData.Values["file"] as string; try { if(string.IsNullOrEmpty(folder)) throw new Exception(); DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Photo/" + folder)); if(!di.Exists) throw New Exception(); if(!string.IsNullOrEmpty(file)) displayImage(di,file); else displayFolder(di); } catch(Exception) { // No debugging made yet, to do, make an alert email for myself! Response.Redirect("/"); } } private void displayImage(DirectoryInfo di, string file) { // TODO: complete this void foreach (FileInfo fi in di.GetFiles("*.jpeg", SearchOption.TopDirectoryOnly)) { ClientScript.RegisterArrayDeclaration("arrImg", string.Format("'{0}'", "/Photo/" + di.Name + "/" + fi.Name)); } ImageButton ib = new ImageButton(); ib.ImageUrl = "/Photo/" + di.Name + "/" + file); ib.CssClass = "preview"; ib.ID = "preview"; ib.ViewStateMode = ViewStateMode.Disabled; photolist.Controls.Add(ib); }Then a javascript will allow me to change the source of the preview imagebutton, without postbacks.
Best Regards
If this post answered your question or solved your problem, please Mark it as Answer.
mm10
Contributor
6409 Points
1184 Posts
Re: Disable viewstate and caching
Apr 10, 2012 03:40 PM|LINK
I don't think you can get rid of the hidden field as it's still sent to the browser to indicate that postback is occurring for the page. You can read more here: http://msdn.microsoft.com/en-us/library/ms178198(v=vs.85).aspx
Trolderik
Member
645 Points
355 Posts
Re: Disable viewstate and caching
Apr 10, 2012 03:46 PM|LINK
Can i minimize the amount of information though?
If this post answered your question or solved your problem, please Mark it as Answer.
mm10
Contributor
6409 Points
1184 Posts
Re: Disable viewstate and caching
Apr 10, 2012 05:16 PM|LINK
You certainly do by setting EnableViewState to false. You cannot turn off the ControlState or get rid of the hidden field though.
If you set EnableViewState and EnableSessionState to false and minimize the number of server controls used to speed up the HTML rendering you should fine.
For more information about ASP.NET performance, please refer to this article: http://msdn.microsoft.com/en-us/library/ms998549.aspx.
Trolderik
Member
645 Points
355 Posts
Re: Disable viewstate and caching
Apr 11, 2012 12:32 PM|LINK
So if I understand your reply correctly, it would reduce the size of the information stored in the ViewState, if I used generic controls over the asp controls? For example
ImageButton ib = new ImageButton(); HyperLink hl = new HyperLink(); HtmlGenericControls div = new HtmlGenericControl("div");If this post answered your question or solved your problem, please Mark it as Answer.
mm10
Contributor
6409 Points
1184 Posts
Re: Disable viewstate and caching
Apr 11, 2012 12:36 PM|LINK
No, to recuce the size of information stored in ViewState simply set EnableViewState to false.
Fever server controls, like for example the GridView, will only make the rendering of the HTML a bit faster.
Trolderik
Member
645 Points
355 Posts
Re: Disable viewstate and caching
Apr 11, 2012 01:19 PM|LINK
oh okay. Could i ask of you to create a small example of how to speed up the following peace of code? I'm not sure exactly how to do it without using server controls.
Default.aspx: <form id="form" runat="server"> <asp:PlaceHolder EnableViewState="false" ID="myplaceholder" runat="server"> </asp:PlaceHolder> </form> Default.aspx.cs: DirectoryInfo di = new DirectoryInfo("C:/somepath/"); foreach (FileInfo fi in di.GetFiles("*.jpg", SearchOptions.TopdirectoryOnly)) { HyperLink hl = new HyperLink(); hl.ImageUrl = di.Name + "/" + fi.Name; hl.NavigateUrl = di.Name + "/" + fi.Name; myplaceholder.Controls.Add(hl); }If this post answered your question or solved your problem, please Mark it as Answer.
mm10
Contributor
6409 Points
1184 Posts
Re: Disable viewstate and caching
Apr 11, 2012 01:31 PM|LINK
The only way I can think of in this example is to set the EnableViewState and EnableSessionState properties of the page to false. There is no need to for example remove the PlaceHolder control as it just renders as a simple DIV.
But if you for example have a page with a GridView control with a lot of columns and data in it, the rendered HTML can get rather heavy. That's what I meant when I suggested fever server controls. In this case, you don't have to worry about the rendering of any controls.