Anas Ghanem

ASP.NET from the middle east

Syndication

Sponsors

News


    Subscribe in a reader
Tip/ Trick: Preventing Session Loss when using Cookieless sessions with TreeView and Menu controls

By default .net runtime uses the cookies to remember the session Id between the requests ,
but when using Cookie less sessions ,the runtime inserts the Session Id to the requested url  ,
this way the runtime can remember the session id and prevent the session loss .

The problem is , when using the Menu and TreeView Controls , these controls doesn't handle this issue ,
so when those controls display there data from the Site maps, they didn't append the session id to the Navigation Urls of there Items ,
and so when the User Navigate to a page using those controls , he will redirected to a Url that didn't contains the session Id,
and so the runtime can't extract the session id , Hence the session will be  lost .

The Solution:
the solution  is to Manually Append the Session Id to NavigateUrl  of the Items for those Navigation controls,
we can use HttpContext.Current.Response.ApplyAppPathModifier to modify the Item Urls as Follows:

For the Menu Control , we can use MenuItemDataBound Event Handler to accomplish this:

protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs e) { // appened the SessionId to Menu Item URL to Avoid sessin loss e.Item.NavigateUrl = HttpContext.Current.Response.ApplyAppPathModifier(e.Item.NavigateUrl); }

For the TreeView Control, we can use TreeNodeDataBound  to accomplish this

 

protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e) { e.Node.NavigateUrl = HttpContext.Current.Response.ApplyAppPathModifier(e.Node.NavigateUrl); }

 

Regards,

Published Saturday, March 08, 2008 11:16 PM by anas

Comments

# re: Tip/ Trick: Preventing Session Loss when using Cookieless sessions with TreeView and Menu controls@ Monday, March 17, 2008 5:33 PM

Thanks a ton for the tip! I was going down the wrong road on this one trying to use menuItemDatabinding. This worked perfectly.

Thanks again.

by Mick

# re: Tip/ Trick: Preventing Session Loss when using Cookieless sessions with TreeView and Menu controls@ Saturday, March 29, 2008 1:03 PM

Great Tip ,

Im' wonder why microsoft Forget this issue !

They must fix there Controls !

Thanks

by Jack

# Avoid session loss when using Cookieless sessions with XML Sitemap Provider@ Wednesday, October 29, 2008 3:56 PM

In march 2008 , I  showed how to avoid losing session when using cookiless sessions with XmlSiteMapPorvider

# re: Tip/ Trick: Preventing Session Loss when using Cookieless sessions with TreeView and Menu controls@ Thursday, January 08, 2009 12:49 AM

great help! thank you so very much dude!

by ace

# re: Tip/ Trick: Preventing Session Loss when using Cookieless sessions with TreeView and Menu controls@ Wednesday, February 11, 2009 6:19 AM

Thanks man...

It worked perfectly.

..

by Kiran

# re: Tip/ Trick: Preventing Session Loss when using Cookieless sessions with TreeView and Menu controls@ Saturday, May 23, 2009 6:48 PM

Nice.... But for the love of God would you ever change the background color of the code!!

by Ross

Leave a Comment

(required) 
(required) 
(optional)
(required)