.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Creating ASPX Pages Dynamically : in a spark

Posted By:Gaurav Arora       Posted Date: August 22, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

Introduction

In a simple creating an aspx page dynamically is a very tedious job. Whenever we think for dynamically creation scenarios stuck in mind about the overload to server etc. But with the help of this article one can attain the same as easily as creating another simple aspx page.

Need and Requirement

Now next words came to mind that what is the need to create an aspx page dynamically? Let me explain it:

  1. You need to create a page, based on selected requirement at the moment.

  2. Need to create a page from static html contents.

  3. Having huge data, need to represent in an individual page and its tough to store in database.

Article Scenario

As stated above, I choose a scenario where you are running a technical community and need to the template where member of your site will able to write some static html contents.

Explanation

For the same we need following pages:

1.PageHeader.ascx

This contains the header information of your page like LOGO etc.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PageHeader.ascx.cs" Inherits="myTemplates_PageHeader" %>

2.PageFooter.ascx

Obvious it contains the Footer information.


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PageFooter.ascx.cs" Inherits="myTemplates_PageFooter" %>
New-York, USA
11 Some Street Second Floor
New York WA 02020
Tel/Fax : 1(800)123-4567
             1(800)123-1234


3.createPage.ascx

Definitely this is the main page, it just create a skeleton as per the above requirement and then segregate the contents and make the title.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="createPage.ascx.cs" Inherits="myTemplates_createPage" %>
    
        
            
            
        
        
            
            
        
        
            
            
        
        
            
            
                Page Title
            
                
            
                Tags
            
                
            
                Description
            
                
            
            
                
                        
  • You may use basic html tags like

  •                          etc.
                        
  • Tags like are NOT allowed.
                        
  • Please check spelling before posting articles.
  •                 
                
            
            
                
                
            
            
                
                
            
           
            
                
                
            
        
                
                    
                
                
                    
                
                
                    
                


  • 4.Default.aspx
    This is the page or container which contains above all 3-controls.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>

    <%@ Register TagPrefix="myPage" TagName="aspxPage" src="~/myTemplates/createPage.ascx" %>
    <%@ Register TagPrefix="myPageHeader" TagName="PgHeader" src="~/myTemplates/PageHeader.ascx" %>
    <%@ Register TagPrefix="myPageFooter" TagName="PgFooter" src="~/myTemplates/PageFooter.ascx" %>
        
        
        
        
            
                
                    
                
                
                    
                
                
                    
                
                
                
                
            
                        
                    
                        

                            Create Dynamic Page(S)
                    
                        
                    
                    
                
        
        

    5.myPageTemplate.tmp
    This is the template page for all pages

    <%@ Page Language="C#" CodeFileBaseClass="PageBase" AutoEventWireup="true" CodeFile ="myPageCode.cs" 
    Inherits= "myPageCode" PageID="[ID]" MetaTitle="[MetaTitle]" MetaKeywords="[MetaKeywords]" %>
    <%@ Register TagPrefix="myPageHeader" TagName="PgHeader" src="~/myTemplates/PageHeader.ascx" %>
    <%@ Register TagPrefix="myPageFooter" TagName="PgFooter" src="~/myTemplates/PageFooter.ascx" %>

        
        
        
        
            
                
                    
                
                
                    
                
                
                    
                
                
                
                
            
                        
                    
                        

                             <%=MetaTitle%>
                    
                        [PageContent]
                    
                    
                
        
        

    6.myPageCode.cs
    A common code-behind page for all dynamic pages

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class myPageCode : PageBase
    {
        public String newPageID
        {
            get
            {
                if (ViewState["newPageID"] != null)
                    return Convert.ToString(ViewState["newPageID"]);
                else
                    return "0";
            }
            set
            {
                ViewState["newPageID"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            base.PageID = this.newPageID;
        }

    }

    7.PageBase.cs
    A pagebase which inherits System.Web.UI.Page.

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Data;
    using System.Configuration;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    /// 
    /// Summary description for PageBase
    /// 
    public class PageBase : System.Web.UI.Page
    {
        public PageBase()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #region Properties
        public String PageID
        {
            get
            {
                if (ViewState["PageID"] != null)
                    return Convert.ToString(ViewState["PageID"]);
                else
                    return "0";
            }
            set
            {
                ViewState["PageID"] = value;
            }
        }
        public String MetaTitle
        {
            get 
            {
                if (ViewState["MetaTitle"] != null)
                    return Convert.ToString(ViewState["MetaTitle"]);
                else
                   return "No Title";
            }
            set
            {
                ViewState["MetaTitle"] = value;
            }
        }
        public String MetaKeywords
        {
            get
            {
                if (ViewState["MetaKeywords"] != null)
                    return Convert.ToString(ViewState["MetaKeywords"]);
                else
                    return "No Keywords";
            }
            set
            {
                ViewState["MetaKeywords"] = value;
            }
        }
        #endregion

        protected override void OnLoad(EventArgs e)
        {
            if (!String.IsNullOrEmpty(MetaTitle))
            {
                HtmlMeta Title = new HtmlMeta();
                Title.Name = "Title";
                Title.Content = this.MetaTitle;
                this.Header.Controls.Add(Title);
            }

            if (!String.IsNullOrEmpty(MetaKeywords))
            {
                HtmlMeta Keyword = new HtmlMeta();
                Keyword.Name = "Keywords";
                Keyword.Content = this.MetaKeywords;
                this.Header.Controls.Add(Keyword);
            }

            //overide base
            base.OnLoad(e);
        }

        void Page_Error(object sender, EventArgs e)
        {
    //Write Some logic here
        }
    }

    Important
    1. Please note that I don't check for any validation
    2. Instead of the use of Custom Control you can use Master Pages too.

    3. The same will create a good facility while you need to create dynamic pages.
    How to run attached code:

    1.Download the zip file
    2.Open your Visual Studio2008
    3.Open WebProject select the specific folder
    4.Press F5 and enjoy

    Your suggestions are invited to improve this.


     Subscribe to Articles

         

    Further Readings:

    Responses
    Author: test         Company URL: http://www.dotnetspark.com
    Posted Date: December 13, 2011

    good

    Post Comment

    You must Sign In To post reply
    Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

    Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend