【发布时间】:2017-02-25 01:46:29
【问题描述】:
我似乎无法在回发时读取会话变量,即使使用以下简单代码:
默认.apsx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" AutoPostBack="true" Text="Button" />
</form>
</body>
</html>
代码隐藏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
varcheck(Session["MyVar"].ToString());
}
}
protected void varcheck(string checker)
{
System.Diagnostics.Debug.WriteLine(checker);
}
protected void Button1_Click(object sender, EventArgs e)
{
string ret_val = "Return this on postback";
Session["MyVar"] = ret_val;
}
}
我明白了:
An exception of type 'System.NullReferenceException' occurred in App_Web_5mcbvlrk.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
我错过了什么吗?会话变量不应该在单击按钮时设置并在回发时读取吗?需要全局设置吗?
【问题讨论】:
-
请停止使用会话
标签: c# asp.net session state postback