Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Web user control problem

Web user control problem

Scheduled Pinned Locked Moved ASP.NET
htmlhelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Taurian110
    wrote on last edited by
    #1

    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 M 2 Replies Last reply
    0
    • T Taurian110

      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 Offline
      I Offline
      Ista
      wrote on last edited by
      #2

      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!!

      T 1 Reply Last reply
      0
      • I Ista

        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!!

        T Offline
        T Offline
        Taurian110
        wrote on last edited by
        #3

        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 was

        tag. 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.

        1 Reply Last reply
        0
        • T Taurian110

          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`
          
          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          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 the Controls collection in the OnInit method of base class Base_Page right before you invoke the Add 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 the FrameworkInitialize 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 the base.Render(writer) in the Render 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 the AddAt method instead of the Add.

          T 1 Reply Last reply
          0
          • M minhpc_bk

            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 the Controls collection in the OnInit method of base class Base_Page right before you invoke the Add 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 the FrameworkInitialize 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 the base.Render(writer) in the Render 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 the AddAt method instead of the Add.

            T Offline
            T Offline
            Taurian110
            wrote on last edited by
            #5

            Thank you very much for your reply, it sure was helpful:-D. Best Regards... Taurian110

            I 1 Reply Last reply
            0
            • T Taurian110

              Thank you very much for your reply, it sure was helpful:-D. Best Regards... Taurian110

              I Offline
              I Offline
              Ista
              wrote on last edited by
              #6

              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!!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups