session variables in javascript on a web form

Hi All,

I have master page on which I have written a javascript which open ups a new window when user clicks on menu item placed on the left side of master page. The javascript follows

 function OpenWind()
       {
         <% if( HttpContext.Current.Session["Id"]==null) { %>
                alert('Session Expired! Redirecting to login page!');
                location.href= 'logout.aspx';
         <% } %>
         var SessionId = <%= HttpContext.Current.Session["Id"].ToString() %>;       
         window.open('adduser.aspx?Mode=E&&Id='+SessionId+'','AddUser',"resizable,scrollbars,menubar,location,status,toolbar,titlebar,scrollbar");
       }

and the calling mark up is this

<a href="#" onclick="javascript:OpenWind();"><asp:Label runat="server" ID="lbl_MyProfile" Text='My Profile'></asp:Label></a>

But this fails gives me object reference not set to an instance error when HttpContext.Current.Session["Id"] is null.

 

0
sirfamol85123
3/30/2009 6:10:52 AM
asp.net.state-management 8807 articles. 0 followers. Follow

4 Replies
1378 Views

Similar Articles

[PageSpeed] 24

from where you are assigning HttpContext.Current.Session["Id"].ToString()  the value ?

get the values if not null

<% if( HttpContext.Current.Session["Id"] !=null) { %>
                var SessionId = <%= HttpContext.Current.Session["Id"].ToString() %>;   

         <% } %>

    


"Never underestimate the power of stupid people in large groups"
0
deesh1531982
3/30/2009 7:30:18 AM

Hello

Assign Session variable like this

Session["Id"] = Context.Session.LCID;

then check as ur condition

<% if( HttpContext.Current.Session["Id"]==null) { %>
                alert('Session Expired! Redirecting to login page!');
                location.href= 'logout.aspx';
         <% } %>
var SessionId = <%= HttpContext.Current.Session["Id"].ToString() %>;  

0
bhushangawale19
3/30/2009 8:33:56 AM

You can't call a method on an object that is not null, so you can't perform a (null).ToString(). I always prefer to maximise the amount of logic in the code-behind wherever possible, so I would prefer to put a method in my code-behind to produce the information needed, but if you want to use inline code do something like this (note that I always code in C#, but you might be able to use an inline IF in VB):

var SessionId = <%= (HttpContext.Current.Session["Id"] == null) ? string.Empty : HttpContext.Current.Session["Id"].ToString() %>;


Andrew
blog.andrewrivers.co.uk
0
wildmoose
3/30/2009 8:35:31 AM

Thanks All,

And sory for relayed reponse.

the code is changed as

 function OpenWind()
       {
         <% if( HttpContext.Current.Session["Id"]==null) { %>
                alert('Session Expired! Redirecting to login page!');
                location.href= 'logout.aspx';
         <% } else { %>
         var SessionId = <%= HttpContext.Current.Session["Id"].ToString() %>;       
         window.open('adduser.aspx?Mode=E&&Id='+SessionId+'','AddUser',"resizable,scrollbars,menubar,location,status,toolbar,titlebar,scrollbar"); 
         <%  }  %>

 }

 And Finally Solved. Thanks All.

 

0
sirfamol85123
4/1/2009 3:07:47 PM
Reply: