Categories
Asp, Asp.net

Dynamic Page Creation Asp.Net

Dynamic Page Creation Asp.Net

Hi friends, This is my first post of Year 2015. I am wishing you all my friends, readers and colleagues A VERY HAPPY NEW YEAR 2015. In this post we are discussing how to create Dynamic Page Creation Asp.Net. To create dynamic page in asp.net we have to follow a few steps:

1. Create a table in SQL Server:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[page_content](
	[page_id] [int] IDENTITY(1,1) NOT NULL,
	[page_location_col] [int] NOT NULL,
	[page_name] [nvarchar](50) NOT NULL,
	page_url [nvarchar] (200) not null,
	[page_content] [nvarchar](max) NOT NULL,
	[enable] [int] NOT NULL,
	[page_creation_date] [datetime] NULL,
 CONSTRAINT [PK_page_content] PRIMARY KEY CLUSTERED 
(
	[page_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] 

GO

ALTER TABLE [dbo].[page_content] ADD  CONSTRAINT [DF_page_content_page_creation_date]  DEFAULT (getdate()) FOR [page_creation_date]
GO

2. Create a master page or a template page(Design page, except them content):
<%@ Page Title="" Language="C#" MasterPageFile="~/Front.master" AutoEventWireup="true" CodeFile="Template.aspx.cs" Inherits="Template" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div class="row">
<div class="col-sm-12">
<h3 id="Template1" runat="server">
</h3>
<hr />
</div>
</div>

<div class="row">
<div class="col-sm-12">
<div id="Template2" runat="server">

</div>
</div>
</div>
</asp:Content>

Template Page Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Media;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class Template : System.Web.UI.Page
{
    String strconn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(strconn);
        con.Open();
        string str = "select page_name,page_content from page_content where page_name='Template'";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        Template1.InnerHtml = dt.Rows[0]["page_name"].ToString();
        Template2.InnerHtml = dt.Rows[0]["page_content"].ToString();
        con.Close();
    }
}

3. Then create dynamic page using template page.
<table align="left" style="font-family:Calibri;font-size:14px">
<tr>
<td colspan="2" align="left">
<h2>Manage Your Profile</h2>
</td>
</tr>
<tr>
<td>Column Number:</td>
<td><asp:DropDownList ID="colno" runat="server">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>

</asp:DropDownList></td>
</tr>
<tr>
<td>Page Name:</td>
<td><asp:TextBox ID="page_name" runat="server" OnTextChanged="page_name_TextChanged" AutoPostBack="true"></asp:TextBox></td>
</tr>
<tr>
<td>Page URL</td>
<td>
<asp:TextBox ID="page_url" runat="server" Enabled="false"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Create_Page" Text="Create Page" runat="server" OnClick="Create_Page_Click" />
</td>
</tr>
</table>

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Media;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class Admin_Page_Creation : System.Web.UI.Page
{
    String strconn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void page_name_TextChanged(object sender, EventArgs e)
    {
        page_url.Text = page_name.Text + ".aspx";
    }
    protected void Create_Page_Click(object sender, EventArgs e)
    {
        File.Copy(Server.MapPath("~/") + "Template.aspx", (Server.MapPath("~/") + "" + page_name.Text + ".aspx"));
        File.Copy(Server.MapPath("~/") + "Template.aspx.cs", (Server.MapPath("~/") + "" + page_name.Text + ".aspx.cs"));

        StreamReader sr3 = new StreamReader(Server.MapPath("~/") + "" + page_name.Text + ".aspx");
        string fileContent3 = sr3.ReadToEnd();
        sr3.Close();
        using (StreamWriter Sw3 = new StreamWriter(Server.MapPath("~/") + "" + page_name.Text + ".aspx"))
        {
            fileContent3 = fileContent3.Replace("Template", page_name.Text);
            Sw3.WriteLine(fileContent3);
            Sw3.Flush();
            Sw3.Close();
        }

        //.aspx.cs Page

        StreamReader sr1 = new StreamReader(Server.MapPath("~/") + "" + page_name.Text + ".aspx.cs");
        string fileContent1 = sr1.ReadToEnd();
        sr1.Close();
        using (StreamWriter Sw1 = new StreamWriter(Server.MapPath("~/") + "" + page_name.Text + ".aspx.cs"))
        {
            fileContent1 = fileContent1.Replace("Template", page_name.Text);
            Sw1.WriteLine(fileContent1);
            Sw1.Flush();
            Sw1.Close();
        }

        SqlConnection con = new SqlConnection(strconn);
        con.Open();
        string str1 = "insert into page_content(page_location_col,page_name,page_content,page_url,enable) values('"+colno.SelectedItem.Text+"','"+page_name.Text+"','No Text','"+page_url.Text+"','1')";
        SqlCommand cmd = new SqlCommand(str1, con);
        cmd.ExecuteNonQuery();
        con.Close();
        

        ScriptManager.RegisterStartupScript(this, this.GetType(), "StartUpScript3", "alert('Page Created Successfully, Add Content Now.')", true);
    }
}

Download Code:
download