L
Lloyd Dupont
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 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"));
}
}
}
=================================