Search code examples
c#asp.netsessionviewstate

Session and View state into asp labels


I have been struggling with getting view state to work in a simple web site. I am just beginning to learn ASP and I have confused myself trying to research ways to implement view states and session. I want to be able to enter text into a text box and change the view state and session. I have successfully got the session to change and print on a page load, but I cant figure out how to print the view state. I am just confusing myself further by trying to look at other examples and I could use some help with making my viewstates and sessions work.

Here is my .cs file:

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 

}

Here is my .aspx file:

<%@ 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>

Any help would be appreciated. Thanks


Solution

  • This is embarrassing as I don't think I was specific enough in my question, but I answered my own question by looking through examples on microsoft.com.. It was hard to explain what I was trying to do here with Session and View state, but essentially I needed to place the session data into a text box from the input of and onclick event. My problems derived from not knowing about how to use: if (IsPostBack). I am still new to ASP and C#. Thanks for the response too, I did need to do some reading on the subject. Anyway here is the code I was looking to create: .cs file:

    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 file:

    <%@ 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>