I needed to keep session alive as long as a customer is viewing a page, no matter if he/she clicks somewhere on the page. With asp.net ajax, this was very easy.
First, you need a ScriptManager on your page with EnablePageMethods set to true:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
</form>
Second, define in your code behind a simple method that does nothing but keeps session alive like this.
[WebMethod]
[ScriptMethod]
public static void KeepAlive()
{
// just keep session alive
}
Note that you have to use WebMethod and ScriptMethod attributes to make this method avaliable for using in scripts.
Third, you have to define javascript methods to call a page method that is going to keep your session alive.
<script type="text/javascript">
function RegisterKeepAlive( sec )
{
setInterval( "PageMethods.KeepAlive( OnComplete, OnTimeOut, OnError )", sec*1000 );
}
function OnComplete( arg ) {}
function OnTimeOut( arg ){}
function OnError( arg ) {}
</script>
Last, just call RegisterKeepAlive with for example 10 seconds on load event of the body:
<body onload="RegisterKeepAlive( 10 )">
You can also use UpdatePanel with Timer control, but this way is better, because you have more options for processing timeout and errors.
Tuesday, March 18, 2008
Monday, March 17, 2008
Great Javascript Resources
Here are some of the javascript resources I found on the net.
Yahoo! User Interface Library - a huge library with many controls and functions, including animation, effects, menus, calendar and so on.
script.aculo.us - has some basic effects that are really nice to use like fading in, fading out, folding, shaking, pulsating and so on.
Smooth Gallery - a great piece of javascript code that is perfect for showing pictures.
Lightbox2 - great for showing photos!
Shadowbox - ideal for photos or galleries, shows even flash and movies.
Carousels for showing large content.
Yahoo! User Interface Library - a huge library with many controls and functions, including animation, effects, menus, calendar and so on.
script.aculo.us - has some basic effects that are really nice to use like fading in, fading out, folding, shaking, pulsating and so on.
Smooth Gallery - a great piece of javascript code that is perfect for showing pictures.
Lightbox2 - great for showing photos!
Shadowbox - ideal for photos or galleries, shows even flash and movies.
Carousels for showing large content.
Master Page PreInit Event Work Around
Because master pages are actually user controls, they do not have PerInit event. But this is the only time when you can change the MasterPageFile property. So, what can we do if we have sophisticated hierarchy of master pages and we need to change the master page of some master page within this master page? The only way to do this is to handle PreInit event of the page, find the appropriate master page and change its master page. But if you still want to have the logic which master page should be used runtime within the master page file, here is one solution to do that with reflection. Override page OnPreInit method and write this code:
// simulates PreInit event for Master pages
MasterPage master = Master;
while ( master != null )
{
MethodInfo mi = master.GetType().GetMethod( "PreInit" );
if ( mi != null )
{
mi.Invoke( master, null );
}
master = master.Master;
}
Then, in the .master file which you would like to change its master page runtime, you should define method like this:
<script runat="server" type="text/C#">
public void PreInit()
{
Account acc = LoggedAccount.GetFromSession( Session );
MasterPageFile = string.Format( "~/local/en-US/{0}/Main.master", acc.Group );
}
</script>
// simulates PreInit event for Master pages
MasterPage master = Master;
while ( master != null )
{
MethodInfo mi = master.GetType().GetMethod( "PreInit" );
if ( mi != null )
{
mi.Invoke( master, null );
}
master = master.Master;
}
Then, in the .master file which you would like to change its master page runtime, you should define method like this:
<script runat="server" type="text/C#">
public void PreInit()
{
Account acc = LoggedAccount.GetFromSession( Session );
MasterPageFile = string.Format( "~/local/en-US/{0}/Main.master", acc.Group );
}
</script>
Subscribe to:
Posts (Atom)
