【发布时间】:2014-07-29 15:17:20
【问题描述】:
我一直在努力让视图状态在一个简单的网站上工作。我才刚刚开始学习 ASP,并且在尝试研究实现视图状态和会话的方法时感到困惑。我希望能够在文本框中输入文本并更改视图状态和会话。我已经成功地让会话在页面加载时更改和打印,但我不知道如何打印视图状态。我只是通过尝试查看其他示例来进一步混淆自己,并且我可以使用一些帮助来使我的视图状态和会话正常工作。
这是我的 .cs 文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Public_Unit3Assignment : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Top of page
Response.Write("Session State: "); //Shows what the current session data is
Response.Write(Session["New"]);
Response.Write("<br />");
Response.Write("Veiwstate: "); //Trying to show what current Viewstate `data is`
Response.Write(ViewState["NameOfUser"]);
Response.Write("<br />");
if (!IsPostBack)
{
string str = "Sample ViewState";
if (ViewState["NameOfUser"] == null)
{
ViewState["NameOfUser"] = str;
}
}
}
//onlcick to display Viewstate
protected void SubmitForm_Click(object sender, EventArgs e)
{
Label2.Text = ViewState["NameOfUser"].ToString();
ViewState["NameOfUser"] = TextBox1.Text; //Try to fill viewstate with text from textbox1
lbl1.Text = TextBox1.Text;
Label2.Text = TextBox1.Text;
}
//Onlcick event to display session
protected void SubmitForm_Click2(object sender, EventArgs e)
{
Session["New"] = SessionTextBox.Text;
Sessionexample.Text = Session["New"].ToString(); //Trying to enter session data into label
Sessionexample.Text = SessionTextBox.Text; //Trying to enter session data into label
SessionResult.Text = SessionTextBox.Text; //Trying to enter session data into label
}
这是我的 .aspx 文件:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/Assignment.master" AutoEventWireup="true" CodeFile="Unit3Assignment.aspx.cs" Inherits="Public_Unit3Assignment" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContentPlaceholder" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="BodyContentPlaceholder" Runat="Server">
<div class="Content">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<br /><br />
<asp:UpdatePanel
id="up1"
runat="server">
<ContentTemplate>
<br />
<br />
<asp:Label
id="lbl1"
text="ViewState Example"
runat="server"></asp:Label>
<br />
<asp:TextBox
id="TextBox1"
Text="ViewState Example TextBox"
runat="Server" />
<br />
<asp:Label
id="Label2"
text="ViewState Result"
runat="server"></asp:Label>
<br />
<asp:TextBox
id="TextBox2"
Text="ViewState Result TextBox"
ReadOnly="true"
runat="Server" />
<br />
<asp:Button
runat="server"
onclick="SubmitForm_Click"
text="Submit & set name" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:UpdatePanel
id="up2"
runat="server">
<ContentTemplate>
<asp:Label
id="Sessionexample"
text="Session Example"
runat="server" />
<br />
<asp:TextBox
id="SessionTextBox"
text="SessionTextBox"
runat="server" />
<br />
<asp:Label
id="SessionResult"
text="Session Result"
runat="server" />
<br />
<asp:TextBox
id="SessionResultTextBox"
text="SessionResultTextBox"
readonly="true"
runat="server" />
<br />
<asp:Button
id="btn2"
runat="server"
onclick="SubmitForm_Click2"
text="Submit for Session"/>
<br />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
任何帮助将不胜感激。谢谢
【问题讨论】:
-
viewstate,不要乱来,asp.net用它来存储控制状态,sesssion状态是用来判断当前会话是否有效,认为你可能会混淆它与会话变量或查询字符串,这两个都可以用来保存会话数据,它总是临时的。
-
您应该避免在页面上下文中使用
Response.Write,因为它会将结果写入文件的末尾(或开头,不记得是哪个)。相反,将文本附加到占位符或其他控件。
标签: c# asp.net session viewstate