Monday, August 10, 2009
Redirection page for error(In Web Config)
Error : 404 page not found
Response.StatusCode = (int)HttpStatusCode.NotFound;
Error : 301 -moved permanently
Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
Response.Write("This page has moved.<br/><a href=" + ReURL + ">Click here</a> to proceed to the new page.");
Response.AddHeader("Location", ReURL);
Response.StatusCode = (int)HttpStatusCode.NotFound;
Error : 301 -moved permanently
Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
Response.Write("This page has moved.<br/><a href=" + ReURL + ">Click here</a> to proceed to the new page.");
Response.AddHeader("Location", ReURL);
Random selection of a row --NEWID() (ms sql)
//Querry for selecting a random field
SELECT top 1 * FROM dbtable where status=0 order by NEWID()
//Example :
SELECT username FROM login order by NEWID();
Try :
SELECT top 15 (ROW_NUMBER() over (order by userid)) as i,username FROM login order by NEWID();
SELECT top 1 * FROM dbtable where status=0 order by NEWID()
//Example :
SELECT username FROM login order by NEWID();
Try :
SELECT top 15 (ROW_NUMBER() over (order by userid)) as i,username FROM login order by NEWID();
Page Refreshing
Refresh after 15 secondes
string RefreahTime="15";
Response.AppendHeader("Refresh",RefreahTime); //using code
<meta http-equiv="refresh" content="15"> //using metatag
string RefreahTime="15";
Response.AppendHeader("Refresh",RefreahTime); //using code
<meta http-equiv="refresh" content="15"> //using metatag
Include contents of an html page in an aspx page
If you are creating a new ASP.NET application, but have a huge collection of existing content in html files, one option is to move all the content into a database and generate pages dynamically. However, migration to a database can be a time-consuming task depending on the volume of content. So wouldn't it be easier to somehow import the relevant parts of the existing html pages into your aspx page?
The use of System.IO and regular expressions makes this a very easy task. Place a <asp:Literal> control (ID="htmlbody") on your page, and then use the following code to strip out everything up to and including the <body> tag (regardless of whether the tag contains additional attributes), and everything from the closing </body> tag onwards:
StreamReader sr;
string html;
sr = File.OpenText("<path_to_file.htm>");
html = sr.ReadToEnd();
sr.Close();
Regex start = new Regex(@"[\s\S]*<body[^<]*>", RegexOptions.IgnoreCase);
html = start.Replace(html,"");
Regex end = new Regex(@"</body[\s\S]*", RegexOptions.IgnoreCase);
html = end.Replace(html, "");
htmlbody.Text = html;
The use of System.IO and regular expressions makes this a very easy task. Place a <asp:Literal> control (ID="htmlbody") on your page, and then use the following code to strip out everything up to and including the <body> tag (regardless of whether the tag contains additional attributes), and everything from the closing </body> tag onwards:
StreamReader sr;
string html;
sr = File.OpenText("<path_to_file.htm>");
html = sr.ReadToEnd();
sr.Close();
Regex start = new Regex(@"[\s\S]*<body[^<]*>", RegexOptions.IgnoreCase);
html = start.Replace(html,"");
Regex end = new Regex(@"</body[\s\S]*", RegexOptions.IgnoreCase);
html = end.Replace(html, "");
htmlbody.Text = html;
Getting HTML from another page(aspx page)
string html;
string newurl = "http://localhost:1997/testpage.aspx";
WebClient c = new WebClient();
Stream s = c.OpenRead(new Uri(newurl));
StreamReader reader = new StreamReader(s);
html = reader.ReadToEnd();
reader.Close();
divid.InnerHtml = html; //This is div id
string newurl = "http://localhost:1997/testpage.aspx";
WebClient c = new WebClient();
Stream s = c.OpenRead(new Uri(newurl));
StreamReader reader = new StreamReader(s);
html = reader.ReadToEnd();
reader.Close();
divid.InnerHtml = html; //This is div id
Getting HTML from ascx in a class
//namespace
using System.Text;
using System.IO;
//public class ThisClass: System.Web.UI.Page//class name should inherit from ystem.Web.UI.Page
public string ReturnContent(string username)
{
Control c = Page.LoadControl("WebUserControl.ascxx");
((UserControlBaseMemberDesign)c).function(username)//function in webusercontrol.ascx for displaying in ascx page
HtmlForm form1 = new HtmlForm();
form1.Controls.Add(c); //a control as html for class
StringWriter sw = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(sw);
string contents = null;
try
{
c.RenderControl(writer);
contents = sw.GetStringBuilder().ToString();
}
finally
{
sw.Close();
writer.Close();
}
return contents;
}
using System.Text;
using System.IO;
//public class ThisClass: System.Web.UI.Page//class name should inherit from ystem.Web.UI.Page
public string ReturnContent(string username)
{
Control c = Page.LoadControl("WebUserControl.ascxx");
((UserControlBaseMemberDesign)c).function(username)//function in webusercontrol.ascx for displaying in ascx page
HtmlForm form1 = new HtmlForm();
form1.Controls.Add(c); //a control as html for class
StringWriter sw = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(sw);
string contents = null;
try
{
c.RenderControl(writer);
contents = sw.GetStringBuilder().ToString();
}
finally
{
sw.Close();
writer.Close();
}
return contents;
}
Getting HTML from ascx and place the html in another aspx page
//namespace
using System.Text;
using System.IO;
Control c = Page.LoadControl("WebUserControl.ascx");
((UserControlBaseSoloDesign)c).function(string username)//function in webusercontrol.ascx for displaying in ascx page
form1.Controls.Add(c); //form 1 is the control in the page
StringWriter sw = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(sw);
string contents = null;
try
{
c.RenderControl(writer);
contents = sw.GetStringBuilder().ToString();
}
finally
{
sw.Close();
writer.Close();
}
Response.Write(contents.ToString());
using System.Text;
using System.IO;
Control c = Page.LoadControl("WebUserControl.ascx");
((UserControlBaseSoloDesign)c).function(string username)//function in webusercontrol.ascx for displaying in ascx page
form1.Controls.Add(c); //form 1 is the control in the page
StringWriter sw = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(sw);
string contents = null;
try
{
c.RenderControl(writer);
contents = sw.GetStringBuilder().ToString();
}
finally
{
sw.Close();
writer.Close();
}
Response.Write(contents.ToString());
Subscribe to:
Posts (Atom)