Saturday, March 08, 2008 11:16 PM anas

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,

Filed under: , ,

Comments

# re: Tip/ Trick: Preventing Session Loss when using Cookieless sessions with TreeView and Menu controls

Monday, March 17, 2008 5:33 PM by Mick

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.

# re: Tip/ Trick: Preventing Session Loss when using Cookieless sessions with TreeView and Menu controls

Saturday, March 29, 2008 1:03 PM by Jack

Great Tip ,

Im' wonder why microsoft Forget this issue !

They must fix there Controls !

Thanks

# Avoid session loss when using Cookieless sessions with XML Sitemap Provider

Wednesday, October 29, 2008 3:56 PM by Anas Ghanem

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 by ace

great help! thank you so very much dude!

# re: Tip/ Trick: Preventing Session Loss when using Cookieless sessions with TreeView and Menu controls

Wednesday, February 11, 2009 6:19 AM by Kiran

Thanks man...

It worked perfectly.

..

# re: Tip/ Trick: Preventing Session Loss when using Cookieless sessions with TreeView and Menu controls

Saturday, May 23, 2009 6:48 PM by Ross

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

Leave a Comment

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