Tuesday, July 29, 2008

Pagebase class for each web site you must include this class

Pagebase class to handle some page base event and function


#region Namespaces
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.IO;
using System.Drawing;
using System.Text.RegularExpressions;
#endregion

namespace DAL
{
public class PageBase : System.Web.UI.Page
{
public static string AppPath = GetAppPath();
#region OnError
///
/// Method Name:-OnError
/// Purpose:- To catch the errors of the .aspx pages and register them in the Errors table
/// Created By:-Snehal
/// Created Date(dd/mm/yyyy):-31/07/2007
///

protected override void OnError(EventArgs e)
{
string strmessage = "";
string strclientip = "";
string strsource = "";

HttpContext ctx = HttpContext.Current;
Exception exception = ctx.Server.GetLastError();

string errorInfo =
"
Offending URL: " + ctx.Request.Url.ToString() +
"
Source: " + exception.Source +
"
Message: " + exception.Message +
"
Stack trace: " + exception.StackTrace;
strmessage = errorInfo;

strclientip = Request.UserHostAddress.ToString();
strsource = ctx.Request.Url.ToString();

//To add the error to the database
int retValue = DAL.Error.AddErrors(strmessage, strclientip, strsource);

//For redirection to the errorpage
string strpath = "~/Admin/ErrorPage.aspx";
Response.Redirect(strpath);
}
#endregion

#region ReadXmlFile
///
/// Method Name:-ReadXmlFile
/// Purpose:- To read the xml file from a given path and return it in the form of dataset.
/// Created By:-Snehal
/// Created Date(dd/mm/yyyy):-31/07/2007
///

public DataSet ReadXmlFile(string xmlfilepath)
{
DataSet ds = new DataSet();
ds.ReadXml(xmlfilepath, XmlReadMode.ReadSchema);
return ds;
}
#endregion

#region CreateTicket
public static void CreateTicket(int Type, string UserName)
{
System.Web.HttpResponse response = HttpContext.Current.Response;
FormsAuthenticationTicket tkt;
string cookiestr;
HttpCookie ck;
if (Type == 1) //Admin
{
tkt = new FormsAuthenticationTicket(1,
UserName,
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,//chkPersistCookie.Checked
"admin");
cookiestr = FormsAuthentication.Encrypt(tkt);
ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
ck.Path = FormsAuthentication.FormsCookiePath;
response.Cookies.Add(ck);

string strRedirect;
strRedirect = HttpContext.Current.Request["ReturnUrl"];
if (strRedirect == null)
{
strRedirect = "Welcome.aspx";
}
else
{
if (strRedirect.ToLower().IndexOf("admin/") <= 0)
{
strRedirect = "Welcome.aspx";
}
}
response.Redirect(strRedirect, true);
}
else if (Type == 2) //SubAdmin
{
tkt = new FormsAuthenticationTicket(1,
UserName,
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,//chkPersistCookie.Checked
"subadmin");
cookiestr = FormsAuthentication.Encrypt(tkt);
ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
ck.Path = FormsAuthentication.FormsCookiePath;
response.Cookies.Add(ck);

string strRedirect;
strRedirect = HttpContext.Current.Request["ReturnUrl"];
if (strRedirect == null)
{
strRedirect = "Welcome.aspx";
}
else
{
if (strRedirect.ToLower().IndexOf("admin/") <= 0)
{
strRedirect = "Welcome.aspx";
}
}
response.Redirect(strRedirect, true);
}
else if (Type == 3) //Member Login
{
tkt = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddMinutes(30), false, "members");
cookiestr = FormsAuthentication.Encrypt(tkt);
ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
ck.Path = FormsAuthentication.FormsCookiePath;
response.Cookies.Add(ck);

string strRedirect;
strRedirect = HttpContext.Current.Request["ReturnUrl"];
if (strRedirect == null)
{
response.Redirect("Default.aspx");
}
}
}
#endregion

#region CheckRole
public static bool CheckRole(string role)
{
bool Rolevalue = false;
try
{
string str_role = ((System.Web.Security.FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData.ToString();
if (str_role == role)
{
Rolevalue = true;
}
}
catch
{ }
return Rolevalue;
}
#endregion

#region ChangeFileName
///
/// Method Name:-ChangeFileName
/// Purpose:- To change the filename of the uploaded image file
/// Created By:-Snehal
/// Created Date(dd/mm/yyyy):-17/04/2007
///

public static string ChangeFileName(string filename)
{
string newFileName;
string[] Substring;
char[] delimitdot = { '.' };
char[] delimitspace = { ' ' };
Substring = filename.Split(delimitdot, 2);
string dateTimenew = System.DateTime.Now.ToString().Replace('/', ' ');
string dateTime = dateTimenew.Replace(':', ' ');
string[] substring1 = dateTime.Split(delimitspace);
string appenddatetime = "";
int i;
for (i = 0; i < substring1.Length - 1; i++)
{
appenddatetime += substring1[i];
}
return newFileName = RemoveSpecialCharactor(Substring[0]) + appenddatetime + '.' + Substring[1];
}
#endregion

#region Create Password
public string CreatePassword()
{
char[] delimitspace = { ' ' };
string dateTimenew = System.DateTime.Now.ToString().Replace('/', ' ');
string dateTime = dateTimenew.Replace(':', ' ');
string[] substring1 = dateTime.Split(delimitspace);
string appenddatetime = "", appenddate = "";
for (int i = 0; i < substring1.Length - 1; i++)
{
appenddatetime += substring1[i];
}
appenddate = Convert.ToString(appenddatetime);
return appenddate;
//string val = appenddate.Length.ToString();
//appenddate = appenddate.Substring(7, 5).ToString() + appenddate.Substring(3, 3).ToString() + appenddate.Substring(0, 4).ToString();
}
#endregion

#region GetMailFile
///
/// Method Name:-GetMailFile
/// Purpose:- To read and get the mail htm file
/// Created By:-Snehal
/// Created Date(dd/mm/yyyy):-23/04/2007
///

public static string GetMailFile(string filepath)
{
string path = filepath;
System.IO.StreamReader sr_Suggestions = new System.IO.StreamReader(path);
string body = sr_Suggestions.ReadToEnd();
sr_Suggestions.Close();
body = body.Replace("[Livepath]", System.Configuration.ConfigurationManager.AppSettings["Livepath"].ToString());
return body;
}
#endregion

#region GetTextonly
public static string GetTextonly(string details)
{
string strtext = "";
strtext = Regex.Replace(details, @"<(.|\n)*?>", string.Empty);
return strtext;
}
#endregion

#region SEO
///
/// Method Name:-SEO
/// Purpose:- To set seo for all the pages of the site
/// Created By:-Snehal
/// Created Date(dd/mm/yyyy):-25/02/2008
///

public static void SEO(int pageid)
{
DataRow dr = null;
dr = DAL.UtilityFunctions.GetSeoCriteria(pageid);
Page page = HttpContext.Current.CurrentHandler as Page;
if (dr != null)
{
if (dr.ItemArray.Length > 0)
{
DAL.UtilityFunctions.SetSEO(dr["title"].ToString(), dr["metakeyword"].ToString(), dr["metadesc"].ToString(), page);
}
}
}
#endregion

#region SEO
///
/// Method Name:-SEO
/// Purpose:- To set seo for all the pages of the site
/// Created By:-Snehal
/// Created Date(dd/mm/yyyy):-25/02/2008
///

public static DataRow SEO(string Keyword)
{
DataTable dt = new DataTable();
DataRow dr = null;
DAL.SqlHelper.FillDataTable(
CommandType.Text,
"select * from dbo.GetSearchSEO('" + Keyword + "')",
dt
);
if (dt != null)
{
if (dt.Rows.Count > 0)
{
dr = dt.Rows[0];
}
}
return dr;
}
#endregion

#region ShowAccessMessage
public void ShowAccessMessage(string Message)
{
((ContentPlaceHolder)Master.FindControl("AdminContent")).Visible = false;
((HtmlTable)Page.Master.FindControl("tbl_access")).Visible = true;
((Literal)Page.Master.FindControl("ltr_Message")).Text = Message;
}
#endregion

#region GetAppPath
public static string GetAppPath()
{
if (HttpContext.Current.Request.ApplicationPath.Length > 1)
return HttpContext.Current.Request.ApplicationPath + "/";
else
return HttpContext.Current.Request.ApplicationPath;
}
#endregion

#region GenerateSectionXML
public static void GenerateSectionXML()
{
//DataSet dt_GenerateSectionXML = new DataSet("Sections");
//SqlHelper.FillDataset(CommandType.StoredProcedure, "GenerateSectionXML", dt_GenerateSectionXML,new string[]{"Section"});
//if (dt_GenerateSectionXML != null)
//{
// dt_GenerateSectionXML.WriteXml(HttpContext.Current.Server.MapPath("~/App_Data/Sections.xml"));
//}
DataTable dt_GenerateSectionXML = new DataTable("Sections");
if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/App_Data/Sections.xml")))
{
File.SetAttributes(HttpContext.Current.Server.MapPath("~/App_Data/Sections.xml"), FileAttributes.Normal);
}
SqlHelper.FillDataTable(CommandType.StoredProcedure, "GenerateSectionXML", dt_GenerateSectionXML);
if (dt_GenerateSectionXML != null)
{
dt_GenerateSectionXML.WriteXml(HttpContext.Current.Server.MapPath("~/App_Data/Sections.xml"), XmlWriteMode.WriteSchema);
}
}
#endregion

#region GetSectionFromXML
///
/// To select section by name
///

///
///
public static DataRow GetSectionFromXML(string SectionName)
{
DataTable dt_Sec = new DataTable();
if (!System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/App_Data/Sections.xml")))
GenerateSectionXML();
dt_Sec.ReadXml(HttpContext.Current.Server.MapPath("~/App_Data/Sections.xml"));
if (dt_Sec.Rows.Count > 0)
{
DataView dv_Sec = dt_Sec.DefaultView;
dv_Sec.RowFilter = "Name = '" + SectionName + "' OR SectionUrl = '" + SectionName + "'";
if (dv_Sec.ToTable().Rows.Count > 0)
{
return dv_Sec.ToTable().Rows[0];
}
}
return null;
}
///
/// To select all section
///

///
public static DataTable GetSectionFromXML(SectionType sType)
{
DataTable dt_Sec = new DataTable();
dt_Sec.ReadXml(HttpContext.Current.Server.MapPath("~/App_Data/Sections.xml"));
if (!System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/App_Data/Sections.xml")))
GenerateSectionXML();
DataView dv_Sec = dt_Sec.DefaultView;
dv_Sec.RowFilter = "Status IN (" + Section.GetSectionStatus(sType) + ")";
return dv_Sec.ToTable();
}
#endregion

#region RemoveSpecialCharactor
///
/// To replace special charactor
///

/// String to replace
/// string Relaced string
public static string RemoveSpecialCharactor(string strToReplace)
{
strToReplace = strToReplace.Replace("~", "");
strToReplace = strToReplace.Replace("`", "");
strToReplace = strToReplace.Replace("!", "");
strToReplace = strToReplace.Replace("@", "");
strToReplace = strToReplace.Replace("#", "");
strToReplace = strToReplace.Replace("$", "");
strToReplace = strToReplace.Replace("%", "");
strToReplace = strToReplace.Replace("^", "");
strToReplace = strToReplace.Replace("&", "");
strToReplace = strToReplace.Replace("*", "");
strToReplace = strToReplace.Replace("(", "");
strToReplace = strToReplace.Replace(")", "");
strToReplace = strToReplace.Replace("-", "");
strToReplace = strToReplace.Replace("_", "");
strToReplace = strToReplace.Replace("+", "");
strToReplace = strToReplace.Replace("=", "");
strToReplace = strToReplace.Replace("[", "");
strToReplace = strToReplace.Replace("]", "");
strToReplace = strToReplace.Replace("{", "");
strToReplace = strToReplace.Replace("}", "");
strToReplace = strToReplace.Replace("|", "");
strToReplace = strToReplace.Replace("\\", "");
strToReplace = strToReplace.Replace(":", "");
strToReplace = strToReplace.Replace(";", "");
strToReplace = strToReplace.Replace("'", "");
strToReplace = strToReplace.Replace("\"", "");
strToReplace = strToReplace.Replace(".", "");
strToReplace = strToReplace.Replace(",", "");
strToReplace = strToReplace.Replace("<", "");
strToReplace = strToReplace.Replace(">", "");
return strToReplace;
}
#endregion
}
}

No comments: