Trying to create a Page by code

L

Lloyd Dupont

In a C# Web Control library I created this Page / HttpHandler:
================
namespace WebUtils
{
public class FTBImageGallery : Page
{
public override void ProcessRequest(HttpContext context)
{
base.ProcessRequest(context);
}
protected override void CreateChildControls()
{
Button but = new Button();
but.Text = "Hello :)";
but.ID = "myButton";
Controls.Add(but);
}
}
}
================

in a web project using this library I wrote this http handler
================
<%@ WebHandler Language="C#" Class="IGallery" %>

using System;
using System.Web;

using WebUtils;

public class IGallery : FTBImageGallery
{
}
================

When I try to query this HttpHandler I get the following error when calling base.ProcessRequest(context)

================
System.Web.HttpUnhandledException was unhandled by user code
Message="Exception of type 'System.Web.HttpUnhandledException' was thrown."
Source="System.Web"
ErrorCode=-2147467259
StackTrace:
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at WebUtils.FTBImageGallery.ProcessRequest(HttpContext context) in F:\MyWork\WebLibrary\WebUtils\WebUtils\FTBImageGallery.cs:line 19
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
================

Any idea of what could have gone wrong?
 
L

Lloyd Dupont

Never mind, thanks to Reflector and the web deployment tool I figured out how to write this working (although still mysterious looking) code. I guess tomorrow I will just have to understand it!
================
public class FTBImageGallery : Page
{
public FTBImageGallery()
{
}
public override void ProcessRequest(HttpContext context)
{
base.ProcessRequest(context);
}
protected override void FrameworkInitialize()
{
base.FrameworkInitialize();
this.__BuildControlTree(this);
//base.AddWrappedFileDependencies(default_aspx.__fileDependencies);
base.Request.ValidateInput();
}
private void __BuildControlTree(FTBImageGallery __ctrl)
{
this.InitializeCulture();
IParserAccessor accessor1 = __ctrl;
accessor1.AddParsedSubObject(new LiteralControl("\r\n\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n\r\n<htmlxmlns=\"http://www.w3.org/1999/xhtml\" >\r\n"));
HtmlHead head1 = this.__BuildControl__control2();
accessor1.AddParsedSubObject(head1);
accessor1.AddParsedSubObject(new LiteralControl("\r\n<body style=\"background-color:#FFFFA0\">\r\n "));
HtmlForm form1 = this.__BuildControlform1();
accessor1.AddParsedSubObject(form1);
accessor1.AddParsedSubObject(new LiteralControl("\r\n</body>\r\n</html>\r\n"));
}
private HtmlForm __BuildControlform1()
{
HtmlForm form1 = new HtmlForm();
form1.ID = "form1";
IParserAccessor accessor1 = form1;
accessor1.AddParsedSubObject(new LiteralControl("\r\n <div>\r\n\t\t"));

Button but = new Button();
but.Text = "Hello :)";
but.ID = "myButton";
accessor1.AddParsedSubObject(but);

return form1;
}
private HtmlHead __BuildControl__control2()
{
HtmlHead head1 = new HtmlHead("head");
HtmlTitle title1 = this.__BuildControl__control3();
IParserAccessor accessor1 = head1;
accessor1.AddParsedSubObject(title1);
return head1;
}
private HtmlTitle __BuildControl__control3()
{
HtmlTitle title1 = new HtmlTitle();
IParserAccessor accessor1 = title1;
accessor1.AddParsedSubObject(new LiteralControl("Untitled Page"));
return title1;
}
}
================
In a C# Web Control library I created this Page / HttpHandler:
================
namespace WebUtils
{
public class FTBImageGallery : Page
{
public override void ProcessRequest(HttpContext context)
{
base.ProcessRequest(context);
}
protected override void CreateChildControls()
{
Button but = new Button();
but.Text = "Hello :)";
but.ID = "myButton";
Controls.Add(but);
}
}
}
================

in a web project using this library I wrote this http handler
================
<%@ WebHandler Language="C#" Class="IGallery" %>

using System;
using System.Web;

using WebUtils;

public class IGallery : FTBImageGallery
{
}
================

When I try to query this HttpHandler I get the following error when calling base.ProcessRequest(context)

================
System.Web.HttpUnhandledException was unhandled by user code
Message="Exception of type 'System.Web.HttpUnhandledException' was thrown."
Source="System.Web"
ErrorCode=-2147467259
StackTrace:
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at WebUtils.FTBImageGallery.ProcessRequest(HttpContext context) in F:\MyWork\WebLibrary\WebUtils\WebUtils\FTBImageGallery.cs:line 19
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
================

Any idea of what could have gone wrong?
 
L

Lloyd Dupont

In the end I came up with that:
(what do you think?)
==============
using System.Web.UI;
using System.Web.UI.HtmlControls;

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


In a C# Web Control library I created this Page / HttpHandler:
================
namespace WebUtils
{
public class FTBImageGallery : Page
{
public override void ProcessRequest(HttpContext context)
{
base.ProcessRequest(context);
}
protected override void CreateChildControls()
{
Button but = new Button();
but.Text = "Hello :)";
but.ID = "myButton";
Controls.Add(but);
}
}
}
================

in a web project using this library I wrote this http handler
================
<%@ WebHandler Language="C#" Class="IGallery" %>

using System;
using System.Web;

using WebUtils;

public class IGallery : FTBImageGallery
{
}
================

When I try to query this HttpHandler I get the following error when calling base.ProcessRequest(context)

================
System.Web.HttpUnhandledException was unhandled by user code
Message="Exception of type 'System.Web.HttpUnhandledException' was thrown."
Source="System.Web"
ErrorCode=-2147467259
StackTrace:
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at WebUtils.FTBImageGallery.ProcessRequest(HttpContext context) in F:\MyWork\WebLibrary\WebUtils\WebUtils\FTBImageGallery.cs:line 19
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
================

Any idea of what could have gone wrong?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top