0

Ok I have a fairly simple issue I want to define some session state variables

So far in my code behind in C# for an aspx page I have the following type of declaration working fine in my index.aspx.cs file.

Session[Constants.firstName] = "Jordan";

or

Session[Constants.firstName] = 123;

I want to grab the Text from the index.aspx Textboxes though instead like so.

Session[Constants.firstName] = txtb_FName.Text;

The problem is that changing this to the variable txtb_FName.Text does not work and when I debug it shows the local variable is being filled with "" not the input from my Textbox.

The whole CODE trail looks like this;

index.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="ProjectPhase02.index" %>

        <!DOCTYPE html>

        <html xmlns="http://www.w3.org/1999/xhtml">
        <head id="Head1" runat="server">
            <title>Project Phase 02</title>
            <link id="Link1" href="main.css" rel="stylesheet" runat="server" />
        </head>
        <body>
            <form id="form1" runat="server" method="get" action="accountCreationConfirmation.aspx">
            <div>
                <asp:Label CssClass="accountFormLable" runat="server" Text="First Name: "></asp:Label>
                <asp:TextBox ID="txtb_FName" runat="server"></asp:TextBox>
                <br />
                <asp:Label CssClass="accountFormLable" runat="server" Text="Last Name: "></asp:Label>
                <asp:TextBox ID="txtb_LName" runat="server"></asp:TextBox>
                <br />
                <asp:Label CssClass="accountFormLable" runat="server" Text="User Name: "></asp:Label>
                <asp:TextBox ID="txtb_UName" runat="server"></asp:TextBox>
                <br />
                <asp:Label CssClass="accountFormLable" runat="server" Text="Password: "></asp:Label>
                <asp:TextBox ID="txtb_Password" runat="server"></asp:TextBox>
                <br />
                <asp:Label CssClass="accountFormLable" runat="server" Text="Address: "></asp:Label>
                <asp:TextBox ID="txtb_Address" runat="server"></asp:TextBox>
                <br />
                <asp:Label CssClass="accountFormLable" runat="server" Text="Email: "></asp:Label>
                <asp:TextBox ID="txtb_Email" runat="server"></asp:TextBox>
                <br />
                <asp:Label CssClass="accountFormLable" runat="server" Text="Phone: "></asp:Label>
                <asp:TextBox ID="txtb_Phone" runat="server"></asp:TextBox>
                <br />

                <asp:Button ID="submitButton" runat="server" Text="Submit" />
            </div>
            </form>
        </body>
        </html>

index.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectPhase02
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session[Constants.firstName] = txtb_FName.Text;
            Session[Constants.lastName] = txtb_LName.Text;
            Session[Constants.userName] = txtb_UName.Text;
            Session[Constants.password] = txtb_Password.Text;
            Session[Constants.address] = txtb_Address.Text;
            Session[Constants.email] = txtb_Email.Text;
            Session[Constants.phoneNumber] = txtb_Phone;
        }
    }
}

Constants.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ProjectPhase02
{
    public static class Constants
    {
        public static string firstName = "thatFName";
        public static string lastName = "thatLName";
        public static string userName = "thatUName";
        public static string password = "thatPass";
        public static string address = "thatAddress";
        public static string email = "thatEmail";
        public static string phoneNumber = "thatPNumber";
    }
}

accountCreationConfirmation.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectPhase02
{
    public partial class accountCreationConfirmation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string fN = (string) Session[Constants.firstName];
            string lN = (string) Session[Constants.lastName];
            string uN = (string) Session[Constants.userName];
            string pW = (string) Session[Constants.password];
            string ad = (string) Session[Constants.address];
            string em = (string) Session[Constants.email];
            string pN = (string) Session[Constants.phoneNumber];

            lbl_FName.Text = fN;
            lbl_LName.Text = lN;
            lbl_UName.Text = uN;
            lbl_Password.Text = pW;
            lbl_Address.Text = ad;
            lbl_Email.Text = em;
            lbl_Phone.Text = pN;
        }
    }
}

accountCreationConfirmation.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="accountCreationConfirmation.aspx.cs" Inherits="ProjectPhase02.accountCreationConfirmation" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Project Phase 02</title>
        <link id="Link1" href="main.css" rel="stylesheet" runat="server" />
    </head>
    <body>
        <form id="form1" runat="server" >
        <div>
            <asp:Label CssClass="accountFormLable" runat="server" Text="First Name: "></asp:Label>
            <asp:Label ID="lbl_FName" runat="server"></asp:Label>
            <br />
            <asp:Label CssClass="accountFormLable" runat="server" Text="Last Name: "></asp:Label>
            <asp:Label ID="lbl_LName" runat="server"></asp:Label>
            <br />
            <asp:Label CssClass="accountFormLable" runat="server" Text="User Name: "></asp:Label>
            <asp:Label ID="lbl_UName" runat="server"></asp:Label>
            <br />
            <asp:Label CssClass="accountFormLable" runat="server" Text="Password: "></asp:Label>
            <asp:Label ID="lbl_Password" runat="server"></asp:Label>
            <br />
            <asp:Label CssClass="accountFormLable" runat="server" Text="Address: "></asp:Label>
            <asp:Label ID="lbl_Address" runat="server"></asp:Label>
            <br />
            <asp:Label CssClass="accountFormLable" runat="server" Text="Email: "></asp:Label>
            <asp:Label ID="lbl_Email" runat="server"></asp:Label>
            <br />
            <asp:Label CssClass="accountFormLable" runat="server" Text="Phone: "></asp:Label>
            <asp:Label ID="lbl_Phone" runat="server"></asp:Label>
            <br />
        </div>
        </form>
    </body>
    </html>

main.css

.accountFormLable {
        width: 100px;
        float: left;
        display: block;
        clear: left;
    }
  • When you debug this, where specifically is the value being lost? It seems really strange to be grabbing the text box values in `Page_Load`. Are you sure you're not over-writing the value with a new blank value by loading the page again or something of that nature? – David Oct 11 '16 at 14:25
  • @EdPlunkett I though that might be the issue I had also tried adding a OnClick "Event" to the button but it seems that the forms "Action" skips that OnClick even entirely. If I set a break point in the On Click named "submitButton_Click" it never reaches that break point. So I'm not sure were to put the code. `protected void submitButton_Click(object sender, EventArgs e) { Session[Constants.firstName] = txtb_FName.Text; }` – Jordan Walker Oct 11 '16 at 20:07
  • @Ed If I hard code in a value to `PageLoad` it does get passed to the second page from `PageLoad`. >> `Session[Constants.firstName] = "TestFirst";` – Jordan Walker Oct 11 '16 at 20:18
  • @JordanWalker Please direct these comments to David who you're actually talking to. All I did was fix your tags. – 15ee8f99-57ff-4f92-890c-b56153 Oct 11 '16 at 20:20
  • @David https://www.youtube.com/watch?v=QD8SVtd4enY&feature=youtu.be In Suzuki's example he Hard codes some values for the session in the page load event. That's fine for just passing static information but I want to pass values from the form I created in Phase01. "TestFirst" is a hard coded first name useful for testing. I tried to setup a button to have an Onclick Event but it seems that the OnClick even never fires. I put in a break for debugging, it never fires that breakpoint skipping the code. Hard coding is the only way I've been able to pass anything using Session so far. – Jordan Walker Oct 12 '16 at 03:40
  • @David I deleted the **Method="get"** and the **Action="accountCreationConfirmation.aspx"** from the
    tag as it was described above in the index page and now the Button Event fires and using **Response.Redirect("accountCreationConfirmation.aspx")** I was able to load the second page with the values.
    – Jordan Walker Oct 12 '16 at 04:53

0 Answers0