Web user control problem
-
I have a BasePage Class from which all the other pages inherit to provide some common features. For header and footer I decided to make a user control which and tried to add that to my BasePage. Here is the code:
public class Base\_Page : Page { public Base\_Page() { } private string \_Title = ""; private string \_BGColor = ""; private bool \_AddHeader = true; protected string Title { get{return \_Title;} set{\_Title = value;} } protected string BGColor { get{return \_BGColor;} set{\_BGColor = value;} } protected bool AddHeader { get{return \_AddHeader;} set{\_AddHeader = value;} } protected override void Render(HtmlTextWriter writer) { // Header writer.WriteLine(""); writer.WriteFullBeginTag("html"); writer.WriteLine(); writer.Indent += 1; writer.WriteFullBeginTag("head"); writer.WriteLine(); writer.Indent += 1; writer.WriteFullBeginTag("title"); writer.Write(Title); writer.WriteEndTag("title"); writer.WriteLine(); writer.Indent -= 1; writer.WriteEndTag("head"); writer.WriteLine(); //writer.WriteFullBeginTag("body"); writer.WriteBeginTag("body"); writer.WriteAttribute("bgColor",BGColor.ToString()); writer.WriteLine(HtmlTextWriter.TagRightChar); writer.WriteLine(); writer.Indent += 1; /\*writer.WriteFullBeginTag("TABLE"); writer.Indent += 1;\*/ // Content base.Render(writer); //Footer /\*writer.WriteEndTag("TABLE"); writer.Indent -= 1;\*/ writer.WriteLine(); writer.Indent -= 1; writer.WriteEndTag("body"); writer.WriteLine(); writer.Indent -= 1; writer.WriteEndTag("html"); } protected override void OnInit(EventArgs e) { Control Header = LoadControl("GlobalControls/Header.ascx"); Controls.Add(Header); base.OnInit(e); } }
For some reason my user control is loading after the page, look at the code below:
`Home Page`
-
I have a BasePage Class from which all the other pages inherit to provide some common features. For header and footer I decided to make a user control which and tried to add that to my BasePage. Here is the code:
public class Base\_Page : Page { public Base\_Page() { } private string \_Title = ""; private string \_BGColor = ""; private bool \_AddHeader = true; protected string Title { get{return \_Title;} set{\_Title = value;} } protected string BGColor { get{return \_BGColor;} set{\_BGColor = value;} } protected bool AddHeader { get{return \_AddHeader;} set{\_AddHeader = value;} } protected override void Render(HtmlTextWriter writer) { // Header writer.WriteLine(""); writer.WriteFullBeginTag("html"); writer.WriteLine(); writer.Indent += 1; writer.WriteFullBeginTag("head"); writer.WriteLine(); writer.Indent += 1; writer.WriteFullBeginTag("title"); writer.Write(Title); writer.WriteEndTag("title"); writer.WriteLine(); writer.Indent -= 1; writer.WriteEndTag("head"); writer.WriteLine(); //writer.WriteFullBeginTag("body"); writer.WriteBeginTag("body"); writer.WriteAttribute("bgColor",BGColor.ToString()); writer.WriteLine(HtmlTextWriter.TagRightChar); writer.WriteLine(); writer.Indent += 1; /\*writer.WriteFullBeginTag("TABLE"); writer.Indent += 1;\*/ // Content base.Render(writer); //Footer /\*writer.WriteEndTag("TABLE"); writer.Indent -= 1;\*/ writer.WriteLine(); writer.Indent -= 1; writer.WriteEndTag("body"); writer.WriteLine(); writer.Indent -= 1; writer.WriteEndTag("html"); } protected override void OnInit(EventArgs e) { Control Header = LoadControl("GlobalControls/Header.ascx"); Controls.Add(Header); base.OnInit(e); } }
For some reason my user control is loading after the page, look at the code below:
`Home Page`
-
Well, this is partly confusing. Its a page, but its a Web Control? What data are you going to add to include as part of every page? That will make it easier? 1 line of code equals many bugs. So don't write any!!
Okay let me try to explain it. My BasePage.cs inherits from System.Web.UI.Page My Webform.aspx inherits from BasePage Insted of adding
Web User Control(Header.ascx and Footer.ascx)
to my Webform.aspx I decided to add it to my BasePage class so all pages may have it. So, I created my Header.ascs and Footer.ascx Web User Controls and tried to add them to my BasePage class. In my webform.aspx the only thing I had wastag. I think this tag is being rendered before my Header.ascx, i dont know why cuz I load controls on
OnInit
Hope that helps. Thanks for the reply anyway. -
I have a BasePage Class from which all the other pages inherit to provide some common features. For header and footer I decided to make a user control which and tried to add that to my BasePage. Here is the code:
public class Base\_Page : Page { public Base\_Page() { } private string \_Title = ""; private string \_BGColor = ""; private bool \_AddHeader = true; protected string Title { get{return \_Title;} set{\_Title = value;} } protected string BGColor { get{return \_BGColor;} set{\_BGColor = value;} } protected bool AddHeader { get{return \_AddHeader;} set{\_AddHeader = value;} } protected override void Render(HtmlTextWriter writer) { // Header writer.WriteLine(""); writer.WriteFullBeginTag("html"); writer.WriteLine(); writer.Indent += 1; writer.WriteFullBeginTag("head"); writer.WriteLine(); writer.Indent += 1; writer.WriteFullBeginTag("title"); writer.Write(Title); writer.WriteEndTag("title"); writer.WriteLine(); writer.Indent -= 1; writer.WriteEndTag("head"); writer.WriteLine(); //writer.WriteFullBeginTag("body"); writer.WriteBeginTag("body"); writer.WriteAttribute("bgColor",BGColor.ToString()); writer.WriteLine(HtmlTextWriter.TagRightChar); writer.WriteLine(); writer.Indent += 1; /\*writer.WriteFullBeginTag("TABLE"); writer.Indent += 1;\*/ // Content base.Render(writer); //Footer /\*writer.WriteEndTag("TABLE"); writer.Indent -= 1;\*/ writer.WriteLine(); writer.Indent -= 1; writer.WriteEndTag("body"); writer.WriteLine(); writer.Indent -= 1; writer.WriteEndTag("html"); } protected override void OnInit(EventArgs e) { Control Header = LoadControl("GlobalControls/Header.ascx"); Controls.Add(Header); base.OnInit(e); } }
For some reason my user control is loading after the page, look at the code below:
`Home Page`
Hi there, The reason the form element is rendered before the header user control is the form control is added to the control hierarchy before the header control though you add it in the
OnInit
method. You can have a look at theControls
collection in theOnInit
method of base classBase_Page
right before you invoke theAdd
method, then you will see this fact. Basically, the form element is added to the control tree very early when the control tree is built in theFrameworkInitialize
method of the dynamic class that represents the web page, you can learn more about this when you have a look at this dynamic class in the temp folder of the ASP.NET application. So when you call thebase.Render(writer)
in theRender
method to render the content of the control hierarchy, the form element is clearly rendered before the header control. In this case, if you want your header control to appear before the form element in the control tree, you simply use theAddAt
method instead of theAdd
. -
Hi there, The reason the form element is rendered before the header user control is the form control is added to the control hierarchy before the header control though you add it in the
OnInit
method. You can have a look at theControls
collection in theOnInit
method of base classBase_Page
right before you invoke theAdd
method, then you will see this fact. Basically, the form element is added to the control tree very early when the control tree is built in theFrameworkInitialize
method of the dynamic class that represents the web page, you can learn more about this when you have a look at this dynamic class in the temp folder of the ASP.NET application. So when you call thebase.Render(writer)
in theRender
method to render the content of the control hierarchy, the form element is clearly rendered before the header control. In this case, if you want your header control to appear before the form element in the control tree, you simply use theAddAt
method instead of theAdd
.Thank you very much for your reply, it sure was helpful:-D. Best Regards... Taurian110
-
Thank you very much for your reply, it sure was helpful:-D. Best Regards... Taurian110
Extending the HttpHandler class, thereby providing a hook into the ASP pipepline would be a much better appraoch to the solution. Inside ProcessRequest, you can add your custom additions for all requests. You would need to modify the Web.config file also. Going even further you can load the ascx page as a ITemplate, giving more control to the design. There is many more possibilities from grabbing the IHttpHandler. LoadContol and LoadTemplete inside TemplateControl will work also. But it sounds like you need to grab the Page in a pre-processing scenario Nick 1 line of code equals many bugs. So don't write any!!