C# - CREATE ASPX Page dynamically

By , 8/24/2012
(1 ratings)
Just create a aspx page and call function with required parameters like showun below

StoreHtml(elm2.InnerHtml, txtPageTitle.Text, txtPageName.Text, txtPageSaveAs.Text);
       public void CreateAspxPageDynalically(string strHtml, string strPageTitle, string strPageName, string strWebsitePath)
        {

            StreamWriter ObjStrWer;
            Boolean bAutoEventWireUpStatus;
            string strLanguage="c#";           
                try
                {
                    //string strnomasterpagedirective = @"<%@ Page Language="+"\" + ddlLanguage.SelectedValue.ToString()+  "\"" + "AutoEventWireup=""true"" %>";
                    string strNoMasterPageDirective = @"<%@ Page Language=" + "\"" + strLanguage + "\"" + " AutoEventWireUp=" + "\"" + bAutoEventWireUpStatus + "\"" + "%>";
                    string strCaching = @"<%@ OutputCache Duration=""800"" VaryByParam=""None"" %>";
                    string strPageHeaderTemplate = @"<!DOCTYPE html PUBLIC" + "\"" + "-//W3C//DTD XHTML 1.0 Transitional//EN" + "\"" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns=" + "\"" + "http://www.w3.org/1999/xhtml" + ">";
                    string strHtmlHeader = @"<head runat=""server""><title>" + strPageTitle + @"</title></head><body><form id=""form1"" runat=""server""><div>";
                    string strHtmlBottom = @"</div></form></body></html>";
                    string strFilePath = @"c:\\";//you can set it                     
                    ObjStrWer = File.CreateText(strFilePath + strPageName);                   
                    //Populate text file from form fields                   
                    //  ObjStrWer.WriteLine(strNoMasterPageDirective);                  
                    ObjStrWer.WriteLine(strPageHeaderTemplate);
                    ObjStrWer.WriteLine(strHtmlHeader);
                    //dvTextPreview.InnerHtml = "<br>" + elm2.InnerText;
                    ObjStrWer.WriteLine(dvTextPreview.InnerHtml); //declare Div or richtextbox to write your webpage content/text
                    ObjStrWer.WriteLine(strHtmlBottom);
                    ObjStrWer.Close();
                    string linkablepath = Regex.Replace(strFilePath, @"\\\\\\", @"\").Replace(@"\\", @"\");
                    lblmsgdisplay.Text = "Aspx Page without MasterPage was Successfully>"; //declare lblmsgdisplay label control for message purpose
                }
                catch (Exception ex)
                {
                    lblmsgdisplay.Text = ex.Message;
                }



 
}
  

Comments

 

Log in, to comment!