C# 创建webforms时未保存C会话
我的编程专业领域不是web编程,但我目前正在通过学习C.net webforms扩展我的知识,我正在编写一个示例。 我有一个按钮,上面有以下代码:C# 创建webforms时未保存C会话,c#,asp.net,session,webforms,C#,Asp.net,Session,Webforms,我的编程专业领域不是web编程,但我目前正在通过学习C.net webforms扩展我的知识,我正在编写一个示例。 我有一个按钮,上面有以下代码: protected void Button1_Click(object sender, EventArgs e) { Course course = new Course(txtId.Text, txtName.Text, txtDesc.Text); // This line checks to see i
protected void Button1_Click(object sender, EventArgs e)
{
Course course = new Course(txtId.Text, txtName.Text, txtDesc.Text);
// This line checks to see if I already have a course manager
cm = (courseManager)Session["CourseManager"];
// If I do not have a course manager, it will create one for me.
if (cm == null)
{
cm = new courseManager();
}
// This will add a course to my list of courses
cm.addCourse(course);
// This will make sure that my web page can see the list of courses.
Session.Add("CourseManager", cm);
// This will reset my text boxes
txtId.Text = "";
txtName.Text = "";
txtDesc.Text = "";
// This will print course to page
//lblId.Text = course.getId();
//lblName.Text = course.getName();
//lblDesc.Text = course.getDesc();
}
然后在网络表单上我有这样一个:
<table id="courses">
<%
if (cm != null)
{
for (int i = 0; i < cm.getCourses().Count(); i++)
{
string id = cm.getCourses().ElementAtOrDefault(i).getId();
string name = cm.getCourses().ElementAtOrDefault(i).getName();
string desc = cm.getCourses().ElementAtOrDefault(i).getDesc();
%>
<tr>
<td><%=id %></td>
<td><%=name %></td>
<td><%=desc %></td>
</tr>
<%
}
}
%>
</table>
有人告诉我,这就是启动活动会话所需的全部内容,但是该会话似乎没有将courseManager类保存到会话中,它会不断创建courseManager类的新实例。我是否缺少c.net webforms在会话中保存项目所需的内容
问候,,
Joey你能在web.config上查找sessionState并粘贴上面的内容吗?另外,您确定没有在其他地方使用空会话[CourseManager]覆盖吗?您如何知道它正在不断创建新实例?您如何在codebehind和网页之间共享cm?此外,您还可以通过检查asp.net_会话的cookie值来检查会话是否被保留。在Chrome>F12>Resources>Cookies>localhost中最容易做到这一点