c# - Initializing a value through a Session variable - Stack Overflow

I need to Initialize a value in a Javascript by using a c# literal that makes reference to a Session Va

I need to Initialize a value in a Javascript by using a c# literal that makes reference to a Session Variable. I am using the following code

<script type="text/javascript" language="javascript" > 
    var myIndex = <%= !((Session["myIndex"]).Equals(null)||(Session["myIndex"]).Equals("")) ? Session["backgroundIndex"] : "1" %>;

However the code above is giving me a classic Object reference not set to an instance of an object. error. Why? Shouldn't (Session["myIndex"]).Equals(null) capture this particular error?

I need to Initialize a value in a Javascript by using a c# literal that makes reference to a Session Variable. I am using the following code

<script type="text/javascript" language="javascript" > 
    var myIndex = <%= !((Session["myIndex"]).Equals(null)||(Session["myIndex"]).Equals("")) ? Session["backgroundIndex"] : "1" %>;

However the code above is giving me a classic Object reference not set to an instance of an object. error. Why? Shouldn't (Session["myIndex"]).Equals(null) capture this particular error?

Share Improve this question asked May 5, 2010 at 10:15 William CallejaWilliam Calleja 4,18511 gold badges43 silver badges51 bronze badges 1
  • 2 Session["myIndex"] returns null, and null does not have an .Equals() function. You need to pare with ==, as that is not a function that needs an object to derive from. – Corey Commented May 5, 2010 at 10:22
Add a ment  | 

5 Answers 5

Reset to default 3

The problem is that null isn't an object, and the Equals() method can only be used on objects. If you want to check if your Session object is null, you should use (Session["myIndex"] == null). You can also use string.IsNullOrEmpty() for an additional check on empty strings. In that case, your code should be:

var myIndex = <%= !string.IsNullOrEmpty((string)Session["myIndex"]) ? Session["backgroundIndex"] : "1" %>;

Note: Shouldn't Session["backgroundIndex"] be Session["myIndex"] in this case? Otherwise the null or empty string check is a bit useless in my opinion.

object reference error may be because (Session["myIndex"]) is null,

(Session["myIndex"]).Equals is used to pare value so you can use it you want to pare like (Session["myIndex"]).Equals("yourIndex")

Are you sure Session["myIndex"] isn't null?

You should add another short circuit OR check for (Session["myIndex"] == null) and get rid of (Session["myIndex"]).Equals(null).

This will work (I have tested it!):

var myIndex = <%=!string.IsNullOrEmpty( (string)Session["myIndex"] ) ? Session["myIndex"] : "1" %> ;

in the code behind create a protected variable and initialize it there. The main advantage is that you can debug it there. And in plus you can use try catch.

code-behind

protected string sessionValue;
private void Page_Load(...)
{
try
{
sessionValue = Session["key"].ToString();
}
catch
{
sessionValue = [defaultValue];
}
}

javascript:

<script>
var sessionValue = "<%= sessionValue %>";
</script>

This way you can avoid the crash and do something else if the sessionValue is null or has a defaultValue.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745433700a4627511.html

相关推荐

  • c# - Initializing a value through a Session variable - Stack Overflow

    I need to Initialize a value in a Javascript by using a c# literal that makes reference to a Session Va

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信