Tuesday, May 5, 2009

Aspx Session use in Java Script

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



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

.........................................

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() %>;

<% } %>


..................................

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() %>;
.........

No comments: