473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

View state problem

The code snippets are below.

I write in (C#) a Page object in a control library.
I'm using this page as an HtppHandler in a website (which referes the DLL).
This page contains 2 panels (one is visible when the other is not).
When I postback to the page (httphandler) the viewstate is apparently incorrectly restored, that is my 2 panels get visible=false (that is only 1 should be invisible) for no reason I could understand.

Any idea of why is that?

PS: I checked the page, there is a viewstate field with some value... which changes between queries...
===== code in the consumer web site =======

=== IGallery.ashx ==
<%@ WebHandler Language="C#" Class="IGallery" %>

using System;
using System.Web;

using WebUtils;

public class IGallery : FTBImageGallery
{
}

===== code of the WebUtils control library ==========

=== FTBImageGallery.cs ===
namespace WebUtils
{
public class FTBImageGallery : CodePage
{
public override void ProcessRequest(HttpContext context)
{
string imageId = context.Request["getimage"];
if (string.IsNullOrEmpty(imageId))
{
base.ProcessRequest(context);
return;
}
WriteImage(context, imageId);
}

Panel pUpload, pSelect;
protected override void CreateChildControls()
{
base.CreateChildControls();
MainForm.Controls.Add(pUpload = CreateUploadPanel());
MainForm.Controls.Add(pSelect = CreateSelectPanel());
pUpload.Visible = false;
}
Panel CreateSelectPanel()
{
Panel p = new Panel();
p.ID = "select";
// ......
return p;
}
Panel CreateUploadPanel()
{
Panel p = new Panel();
p.ID = "upload";
// ......
return p;
}
}
}

=== CodePage.cs ===
namespace WebUtils
{
public class CodePage : Page
{
public bool ValidateRequest = false;

protected override void FrameworkInitialize()
{
base.FrameworkInitialize();
InitializeCulture();
BuildPage();
Title = Title;
if(ValidateRequest)
Request.ValidateInput();
}

public readonly HtmlForm MainForm = new HtmlForm();
public readonly HtmlHead Head = new HtmlHead();
HtmlTitle pageTitle = new HtmlTitle();
public readonly HtmlGenericControl Body = new HtmlGenericControl("body");

public new string Title
{
get { return base.Title; }
set
{
pageTitle.Text = value;
base.Title = value;
}
}

protected virtual void BuildPage()
{
Head.Controls.Add(pageTitle);
Body.Controls.Add(MainForm);
MainForm.Controls.Add(new LiteralControl("\r\n"));

Controls.Add(new LiteralControl("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\" >\r\n"));
Controls.Add(Head);
Controls.Add(new LiteralControl("\r\n"));
Controls.Add(Body);
Controls.Add(new LiteralControl("\r\n</html>\r\n"));
}
}
}

=================================
--
I have taken a vow of poverty. If you want to really piss me off, send me money.

Jan 19 '06 #1
4 1763
The problem seems to be triggered by a LinkButton I have on the page in fact.
it has the following reasonable looking code:

LinkButton linkSelectUpload;
protected override void CreateChildControls()
{
base.CreateChildControls();

linkSelectUpload = new LinkButton();
linkSelectUpload.Text = "Upload Images";
linkSelectUpload.Click += HSwitchSelectUpload;
MainForm.Controls.Add(linkSelectUpload);

MainForm.Controls.Add(pUpload = CreateUploadPanel());
MainForm.Controls.Add(pSelect = CreateSelectPanel());
pUpload.Visible = false;
}
void HSwitchSelectUpload(object sender, EventArgs e)
{
pUpload.Visible = !pUpload.Visible;
pSelect.Visible = !pSelect.Visible;
linkSelectUpload.Text = pUpload.Visible ? "Select Image" : "Upload Images";
}
"Lloyd Dupont" <net.galador@ld> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...
The code snippets are below.

I write in (C#) a Page object in a control library.
I'm using this page as an HtppHandler in a website (which referes the DLL).
This page contains 2 panels (one is visible when the other is not).
When I postback to the page (httphandler) the viewstate is apparently incorrectly restored, that is my 2 panels get visible=false (that is only 1 should be invisible) for no reason I could understand.

Any idea of why is that?

PS: I checked the page, there is a viewstate field with some value... which changes between queries...
===== code in the consumer web site =======

=== IGallery.ashx ==
<%@ WebHandler Language="C#" Class="IGallery" %>

using System;
using System.Web;

using WebUtils;

public class IGallery : FTBImageGallery
{
}

===== code of the WebUtils control library ==========

=== FTBImageGallery.cs ===
namespace WebUtils
{
public class FTBImageGallery : CodePage
{
public override void ProcessRequest(HttpContext context)
{
string imageId = context.Request["getimage"];
if (string.IsNullOrEmpty(imageId))
{
base.ProcessRequest(context);
return;
}
WriteImage(context, imageId);
}

Panel pUpload, pSelect;
protected override void CreateChildControls()
{
base.CreateChildControls();
MainForm.Controls.Add(pUpload = CreateUploadPanel());
MainForm.Controls.Add(pSelect = CreateSelectPanel());
pUpload.Visible = false;
}
Panel CreateSelectPanel()
{
Panel p = new Panel();
p.ID = "select";
// ......
return p;
}
Panel CreateUploadPanel()
{
Panel p = new Panel();
p.ID = "upload";
// ......
return p;
}
}
}

=== CodePage.cs ===
namespace WebUtils
{
public class CodePage : Page
{
public bool ValidateRequest = false;

protected override void FrameworkInitialize()
{
base.FrameworkInitialize();
InitializeCulture();
BuildPage();
Title = Title;
if(ValidateRequest)
Request.ValidateInput();
}

public readonly HtmlForm MainForm = new HtmlForm();
public readonly HtmlHead Head = new HtmlHead();
HtmlTitle pageTitle = new HtmlTitle();
public readonly HtmlGenericControl Body = new HtmlGenericControl("body");

public new string Title
{
get { return base.Title; }
set
{
pageTitle.Text = value;
base.Title = value;
}
}

protected virtual void BuildPage()
{
Head.Controls.Add(pageTitle);
Body.Controls.Add(MainForm);
MainForm.Controls.Add(new LiteralControl("\r\n"));

Controls.Add(new LiteralControl("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\" >\r\n"));
Controls.Add(Head);
Controls.Add(new LiteralControl("\r\n"));
Controls.Add(Body);
Controls.Add(new LiteralControl("\r\n</html>\r\n"));
}
}
}

=================================
--
I have taken a vow of poverty. If you want to really piss me off, send me money.

Jan 19 '06 #2
Haha..
There are some HtmlInputFile which seems to be the cause of the disappearance of the ViewState...
Any idea why?
"Lloyd Dupont" <net.galador@ld> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...
The code snippets are below.

I write in (C#) a Page object in a control library.
I'm using this page as an HtppHandler in a website (which referes the DLL).
This page contains 2 panels (one is visible when the other is not).
When I postback to the page (httphandler) the viewstate is apparently incorrectly restored, that is my 2 panels get visible=false (that is only 1 should be invisible) for no reason I could understand.

Any idea of why is that?

PS: I checked the page, there is a viewstate field with some value... which changes between queries...
===== code in the consumer web site =======

=== IGallery.ashx ==
<%@ WebHandler Language="C#" Class="IGallery" %>

using System;
using System.Web;

using WebUtils;

public class IGallery : FTBImageGallery
{
}

===== code of the WebUtils control library ==========

=== FTBImageGallery.cs ===
namespace WebUtils
{
public class FTBImageGallery : CodePage
{
public override void ProcessRequest(HttpContext context)
{
string imageId = context.Request["getimage"];
if (string.IsNullOrEmpty(imageId))
{
base.ProcessRequest(context);
return;
}
WriteImage(context, imageId);
}

Panel pUpload, pSelect;
protected override void CreateChildControls()
{
base.CreateChildControls();
MainForm.Controls.Add(pUpload = CreateUploadPanel());
MainForm.Controls.Add(pSelect = CreateSelectPanel());
pUpload.Visible = false;
}
Panel CreateSelectPanel()
{
Panel p = new Panel();
p.ID = "select";
// ......
return p;
}
Panel CreateUploadPanel()
{
Panel p = new Panel();
p.ID = "upload";
// ......
return p;
}
}
}

=== CodePage.cs ===
namespace WebUtils
{
public class CodePage : Page
{
public bool ValidateRequest = false;

protected override void FrameworkInitialize()
{
base.FrameworkInitialize();
InitializeCulture();
BuildPage();
Title = Title;
if(ValidateRequest)
Request.ValidateInput();
}

public readonly HtmlForm MainForm = new HtmlForm();
public readonly HtmlHead Head = new HtmlHead();
HtmlTitle pageTitle = new HtmlTitle();
public readonly HtmlGenericControl Body = new HtmlGenericControl("body");

public new string Title
{
get { return base.Title; }
set
{
pageTitle.Text = value;
base.Title = value;
}
}

protected virtual void BuildPage()
{
Head.Controls.Add(pageTitle);
Body.Controls.Add(MainForm);
MainForm.Controls.Add(new LiteralControl("\r\n"));

Controls.Add(new LiteralControl("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\" >\r\n"));
Controls.Add(Head);
Controls.Add(new LiteralControl("\r\n"));
Controls.Add(Body);
Controls.Add(new LiteralControl("\r\n</html>\r\n"));
}
}
}

=================================
--
I have taken a vow of poverty. If you want to really piss me off, send me money.

Jan 19 '06 #3
Solved!!!

It probably has to do as to when the view state is applied.
If I set panel.Visible = false before adding it to the page, it works!
"Lloyd Dupont" <net.galador@ld> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...
The code snippets are below.

I write in (C#) a Page object in a control library.
I'm using this page as an HtppHandler in a website (which referes the DLL).
This page contains 2 panels (one is visible when the other is not).
When I postback to the page (httphandler) the viewstate is apparently incorrectly restored, that is my 2 panels get visible=false (that is only 1 should be invisible) for no reason I could understand.

Any idea of why is that?

PS: I checked the page, there is a viewstate field with some value... which changes between queries...
===== code in the consumer web site =======

=== IGallery.ashx ==
<%@ WebHandler Language="C#" Class="IGallery" %>

using System;
using System.Web;

using WebUtils;

public class IGallery : FTBImageGallery
{
}

===== code of the WebUtils control library ==========

=== FTBImageGallery.cs ===
namespace WebUtils
{
public class FTBImageGallery : CodePage
{
public override void ProcessRequest(HttpContext context)
{
string imageId = context.Request["getimage"];
if (string.IsNullOrEmpty(imageId))
{
base.ProcessRequest(context);
return;
}
WriteImage(context, imageId);
}

Panel pUpload, pSelect;
protected override void CreateChildControls()
{
base.CreateChildControls();
MainForm.Controls.Add(pUpload = CreateUploadPanel());
MainForm.Controls.Add(pSelect = CreateSelectPanel());
pUpload.Visible = false;
}
Panel CreateSelectPanel()
{
Panel p = new Panel();
p.ID = "select";
// ......
return p;
}
Panel CreateUploadPanel()
{
Panel p = new Panel();
p.ID = "upload";
// ......
return p;
}
}
}

=== CodePage.cs ===
namespace WebUtils
{
public class CodePage : Page
{
public bool ValidateRequest = false;

protected override void FrameworkInitialize()
{
base.FrameworkInitialize();
InitializeCulture();
BuildPage();
Title = Title;
if(ValidateRequest)
Request.ValidateInput();
}

public readonly HtmlForm MainForm = new HtmlForm();
public readonly HtmlHead Head = new HtmlHead();
HtmlTitle pageTitle = new HtmlTitle();
public readonly HtmlGenericControl Body = new HtmlGenericControl("body");

public new string Title
{
get { return base.Title; }
set
{
pageTitle.Text = value;
base.Title = value;
}
}

protected virtual void BuildPage()
{
Head.Controls.Add(pageTitle);
Body.Controls.Add(MainForm);
MainForm.Controls.Add(new LiteralControl("\r\n"));

Controls.Add(new LiteralControl("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\" >\r\n"));
Controls.Add(Head);
Controls.Add(new LiteralControl("\r\n"));
Controls.Add(Body);
Controls.Add(new LiteralControl("\r\n</html>\r\n"));
}
}
}

=================================
--
I have taken a vow of poverty. If you want to really piss me off, send me money.

Jan 19 '06 #4
sam
Yeah, I know why this is.

You have to know about the LoadViewState event. See this page:
http://www.15seconds.com/issue/020102.htm

Basically, what you *were* doing in overriding the restored view state
value by setting visibility = false *after* the viewstate was restored
properly. So pUpload visibility was always being restored properly by
viewstate, but you were always setting it to false right after that
(LoadViewState event occurs when you add pUpload to the Controls
collection - controls play catchup on lifecycle events when added to a
preexisting control tree). But when you set it to false *before*
adding it to the controls collection you gave a chance for viewstate to
be restored to it properly, since you stopped overriding it right after
that.

Make sense? Basically this ASP.NET stuff is a lot more complicated
than a lot of ppl say it is ;).

-Sam

Jan 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bren | last post by:
I am developing a web based app. in .net, with C# as the code behind.the problem I am having is with view state for a page. the page is capturing an event fired in an object. the event changes the...
0
by: Bren | last post by:
I am developing a web based app. in .net, with C# as the code behind.the problem I am having is with view state for a page. the page is capturing an event fired in an object. the event changes the...
0
by: Eran Amitai | last post by:
I have the simplest web page (using code-behind): <body> <%# MyText %> two buttons - button1 and button2 </body> My code-behind web page is very simple too: public class WebForm3 :...
2
by: Vinod I | last post by:
Hi Team, When I tryed following code, I am getting the Runtime Error as "The View State is invalid for this page and might be corrupted." Exception Details: System.Web.HttpException: The View...
2
by: Brad | last post by:
I have an intranet app that has just started sporadically getting the following error "The viewstate is invalid for this page and might be corrupted." By sproadic I mean 3-4 times during the past...
3
by: Philip Tripp | last post by:
I've read numerous sources stating that view state can be disabled per control, and per page, but can't seem to keep web form controls from remembering their state on a postback. I'm using VS.Net...
2
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to...
2
by: Hope Paka | last post by:
My web page, has an average of 1500 hits every day. Every day, i get invalid view state exception, average of 10 times. The exception message in at the bottom. When i try to parse the view state in...
1
by: Ken Varn | last post by:
I have a problem where my DataGrid would not maintain the ViewState of my databound rows. I finally narrowed down the problem. If my first column is a template column, the view state for the...
1
by: =?Utf-8?B?Tmls?= | last post by:
Hi, I am facing problem with the huge Viewstate size(Using .NET 2.0). to improve the application performance, I am using Static variable to store view state data. Does anyone has other option to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.