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,

6 Comments

Comments have been disabled for this content.