Here is the simple code to convert ASPX page to ASPX user control
public class ConvertPageToUserControl
{
string _pageFileName = string.Empty;
string _pagePath = string.Empty;
StreamReader _pageRader = default(StreamReader);
StreamWriter _ucWriter = default(StreamWriter);
public string PageFileName
{
get
{ return _pageFileName; }
set
{ _pageFileName = value; }
}
public string PageFilePath
{
get
{ return _pagePath; }
set
{ _pagePath = value; }
}
string PageFile
{
get { return Path.Combine(_pagePath,_pageFileName); }
}
string UserControlName
{
get { return string.Format("{0}.ascx", _pageFileName.Split(".".ToCharArray())[0]); }
}
string UserControlFile
{
get { return Path.Combine(_pagePath, UserControlName); }
}
public void Convert()
{
ReadPageDoc();
GetUCDoc();
string _lineText = _pageRader.ReadLine();
while (!_pageRader.EndOfStream)
{
if (_lineText.ToLower().IndexOf("<html") > -1 ||
(_lineText.ToLower().IndexOf("<body") > -1) ||
(_lineText.ToLower().IndexOf("<head") > -1) ||
(_lineText.ToLower().IndexOf("<title") > -1) ||
(_lineText.ToLower().IndexOf("<form") > -1) ||
_lineText.ToLower().IndexOf("</html") > -1 ||
(_lineText.ToLower().IndexOf("</body") > -1) ||
(_lineText.ToLower().IndexOf("</head") > -1) ||
(_lineText.ToLower().IndexOf("</title") > -1) ||
(_lineText.ToLower().IndexOf("</form") > -1) ||
(_lineText.ToUpper().IndexOf("<!DOCTYPE") > -1))
{
_lineText = _pageRader.ReadLine();
continue;
}
else if (_lineText.ToLower().IndexOf("@ page") > -1)
_lineText = UpdatePageTag(_lineText);
_ucWriter.WriteLine(_lineText);
_lineText = _pageRader.ReadLine();
}
_ucWriter.Close();
_pageRader.Close();
StreamReader _pageCodeBehind = new StreamReader(PageFile + ".cs");
StreamWriter _ucCodeBehind = new StreamWriter(UserControlFile + ".cs");
while (!_pageCodeBehind.EndOfStream)
{
string LineText = string.Empty;
LineText = _pageCodeBehind.ReadLine();
LineText = LineText.Replace("System.Web.UI.Page", "System.Web.UI.UserControl");
_ucCodeBehind.WriteLine(LineText);
}
_pageCodeBehind.Close();
_ucCodeBehind.Close();
}
string UpdatePageTag(string InputLine)
{
string OutPutLine = "<%@ Control Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"{0}\" Inherits=\"{1}\" %>";
string CodeBehind = string.Empty;
string Inherits = string.Empty;
string TransformedText = InputLine.Replace(" ", "");
int indexStart = 0;
//getting CodeBehind
indexStart = TransformedText.IndexOf("CodeBehind=\"");
indexStart = indexStart + "CodeBehind=\"".Length;
TransformedText = TransformedText.Substring(indexStart);
CodeBehind = TransformedText.Split("\"".ToCharArray())[0];
CodeBehind = CodeBehind.Replace("aspx", "ascx");
TransformedText = InputLine.Replace(" ", "");
indexStart = 0;
//getting Inherits
indexStart = TransformedText.IndexOf("Inherits=\"");
indexStart = indexStart + "Inherits=\"".Length;
TransformedText = TransformedText.Substring(indexStart);
Inherits = TransformedText.Split("\"".ToCharArray())[0];
return string.Format(OutPutLine,CodeBehind ,Inherits);
}
void GetUCDoc()
{
_ucWriter = new StreamWriter(UserControlFile);
}
void ReadPageDoc()
{
_pageRader = new StreamReader(PageFile);
}
}
{
string _pageFileName = string.Empty;
string _pagePath = string.Empty;
StreamReader _pageRader = default(StreamReader);
StreamWriter _ucWriter = default(StreamWriter);
public string PageFileName
{
get
{ return _pageFileName; }
set
{ _pageFileName = value; }
}
public string PageFilePath
{
get
{ return _pagePath; }
set
{ _pagePath = value; }
}
string PageFile
{
get { return Path.Combine(_pagePath,_pageFileName); }
}
string UserControlName
{
get { return string.Format("{0}.ascx", _pageFileName.Split(".".ToCharArray())[0]); }
}
string UserControlFile
{
get { return Path.Combine(_pagePath, UserControlName); }
}
public void Convert()
{
ReadPageDoc();
GetUCDoc();
string _lineText = _pageRader.ReadLine();
while (!_pageRader.EndOfStream)
{
if (_lineText.ToLower().IndexOf("<html") > -1 ||
(_lineText.ToLower().IndexOf("<body") > -1) ||
(_lineText.ToLower().IndexOf("<head") > -1) ||
(_lineText.ToLower().IndexOf("<title") > -1) ||
(_lineText.ToLower().IndexOf("<form") > -1) ||
_lineText.ToLower().IndexOf("</html") > -1 ||
(_lineText.ToLower().IndexOf("</body") > -1) ||
(_lineText.ToLower().IndexOf("</head") > -1) ||
(_lineText.ToLower().IndexOf("</title") > -1) ||
(_lineText.ToLower().IndexOf("</form") > -1) ||
(_lineText.ToUpper().IndexOf("<!DOCTYPE") > -1))
{
_lineText = _pageRader.ReadLine();
continue;
}
else if (_lineText.ToLower().IndexOf("@ page") > -1)
_lineText = UpdatePageTag(_lineText);
_ucWriter.WriteLine(_lineText);
_lineText = _pageRader.ReadLine();
}
_ucWriter.Close();
_pageRader.Close();
StreamReader _pageCodeBehind = new StreamReader(PageFile + ".cs");
StreamWriter _ucCodeBehind = new StreamWriter(UserControlFile + ".cs");
while (!_pageCodeBehind.EndOfStream)
{
string LineText = string.Empty;
LineText = _pageCodeBehind.ReadLine();
LineText = LineText.Replace("System.Web.UI.Page", "System.Web.UI.UserControl");
_ucCodeBehind.WriteLine(LineText);
}
_pageCodeBehind.Close();
_ucCodeBehind.Close();
}
string UpdatePageTag(string InputLine)
{
string OutPutLine = "<%@ Control Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"{0}\" Inherits=\"{1}\" %>";
string CodeBehind = string.Empty;
string Inherits = string.Empty;
string TransformedText = InputLine.Replace(" ", "");
int indexStart = 0;
//getting CodeBehind
indexStart = TransformedText.IndexOf("CodeBehind=\"");
indexStart = indexStart + "CodeBehind=\"".Length;
TransformedText = TransformedText.Substring(indexStart);
CodeBehind = TransformedText.Split("\"".ToCharArray())[0];
CodeBehind = CodeBehind.Replace("aspx", "ascx");
TransformedText = InputLine.Replace(" ", "");
indexStart = 0;
//getting Inherits
indexStart = TransformedText.IndexOf("Inherits=\"");
indexStart = indexStart + "Inherits=\"".Length;
TransformedText = TransformedText.Substring(indexStart);
Inherits = TransformedText.Split("\"".ToCharArray())[0];
return string.Format(OutPutLine,CodeBehind ,Inherits);
}
void GetUCDoc()
{
_ucWriter = new StreamWriter(UserControlFile);
}
void ReadPageDoc()
{
_pageRader = new StreamReader(PageFile);
}
}
No comments:
Post a Comment