我一直在努力让视图状态在一个简单的网站上工作。我刚刚开始学习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>
任何帮助将不胜感激。感谢
答案 0 :(得分:0)
这很令人尴尬,因为我不认为我在问题上有足够的具体内容,但我通过查看microsoft.com上的示例回答了我自己的问题。很难解释我在这里尝试做什么具有会话和视图状态,但基本上我需要将会话数据放入输入和onclick事件的文本框中。我的问题源于不知道如何使用:if(IsPostBack)。我还是ASP和C#的新手。感谢您的回复,我确实需要对这个主题进行一些阅读。无论如何这里是我想要创建的代码: .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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//session set
Session["message"] = "Hello World";
//session get
lblMessage.Text = Session["message"].ToString();
//viewstate set
ViewState["message"] = "Hi there world";
//viewstate get
lblMessageView.Text = ViewState["message"].ToString();
}
protected void SubmitForm_Click(object sender, EventArgs e)
{
if (IsPostBack)
{
ViewState["New"] = TextView1.Text; //makes new viewstate with text result
TextView2.Text = ViewState["New"].ToString(); //Trying to enter viewstate data in text box
}
}
protected void SubmitForm_Click2(object sender, EventArgs e)
{
if (IsPostBack)
{
Session["New"] = SessionTextBox.Text; //makes new session with text result
SessionResultTextBox.Text = Session["New"].ToString(); //Trying to enter session data into text box
}
}
}
.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">
<asp:Label
id="session"
runat="server"
text="Session Message: " />
<asp:Label
id="lblMessage"
runat="server"
backcolor="Yellow"
forecolor="Black" />
<br />
<asp:Label
id="viewstate"
runat="server"
text="ViewState Message: " />
<asp:Label
id="lblMessageView"
runat="server"
backcolor="red"
forecolor="green" />
<br /><br /><br />
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel
id="up1"
runat="server" >
<ContentTemplate>
<br />
<asp:Label
id="View1"
text="ViewState Example"
runat="server" />
<br />
<asp:TextBox
id="TextView1"
text="ViewStateTextBox"
runat="server" />
<br />
<asp:Label
id="View2"
text="ViewState Result"
runat="server" />
<br />
<asp:TextBox
id="TextView2"
text="ViewStateResultTextBox"
runat="server" />
<br />
<asp:Button
id="btn1"
runat="server"
onclick="SubmitForm_Click"
text="Show ViewState"/>
</ContentTemplate>
</asp:UpdatePanel>
<br /><br /><br />
<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>