Monday, October 08, 2007

Providing Google like "Processing........" status

If you wanted to provide status bar for your pages as in Google.
you cab use below code to achieve the same.
Scnario:
1.Currently you are on Page1(start.aspx).Your post back leads you to Page2(End.aspx)
2.End.aspx is taking a while to load, so you want to show some status as in shown in Gmail.

Below screen shot will explain the same in detail.
Start.aspx


In Code behind file of Start.aspx button click event handler looks like this.

public void BtnClick(object sender, System.EventArgs e)
{
// Write your code here
string url = "Inter.aspx?target=End.aspx";
HttpContext.Current.Response.Redirect(url);
}



Inter.aspx just consist simple mesage as below.


Inter.aspx.cs looks like below.
public class Inter : System.Web.UI.Page
{
protected HtmlGenericControl bd;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
HtmlGenericControl body = (HtmlGenericControl)Page.FindControl("bd");
if (body != null)
{
body.Attributes["onload"] = GetAutoRedirectScript();
}

}

public string GetAutoRedirectScript()
{
return String.Format("location.href='{0}';", "End.aspx");

}
}

Friday, August 31, 2007

Firing/Invoking Multiple Javascript fuctions on body onload

Often we land up in a scenario, where we require to invoke more than 2 javascript functions on Page Load.
We can call one function straight away by calling a function on 'onload' event of body tag. 2nd function u can invoke by overriding Render event of page life cycle. You can over-ride 'Render' event as below.
protected override void Render (HtmlTextWriter writer)
{
    base.Render (writer);
    writer.Write("");
}
Note:
Developers often try to achieve the same functionality by calling javascript function as below.
    Response.Write("");
It results in an error, because "Response.Write" tries to call the function even before page load happens, hence it can't find/locate required javascript fuction in page, which results in an error.

Friday, July 13, 2007

Creating JavaScript Member functions

Syntax for creating your member functions in JavaScript

String.prototype.FunctionName = function()
{
// write your logic;
return 'return value';
}

You can convvert above mentioned ToAscii fuction as member function, Thats left for the readers for practice.

Here are some frequently used functions.
Examples:
1.Trim
String.prototype.Trim = function()
{
return this.replace(/^\s+|\s+$/g,"");
}
2.Ltrim
String.prototype.Ltrim = function()
{
return this.replace(/^\s+/,"");
}
3.Rtrim
String.prototype.rtrim = function()
{
return this.replace(/\s+$/,"");
}