Thursday, November 29, 2012

Session Variables

Here is Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnGetAdditionProblem" runat="server" Text="Addition" 
            onclick="btnGetAdditionProblem_Click" /><br />
        <asp:Label ID="lblProblem" runat="server" Text="Label"></asp:Label><br/>
        <asp:TextBox ID="txtAnswer" runat="server"></asp:TextBox>
        <asp:Button ID="btnCheckAnswer" runat="server" Text="Check" 
            onclick="btnCheckAnswer_Click" /><br />
        <asp:Label ID="lblResult" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

Here is the code behind Default.aspx.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
{
    /*This class does a test for simple addition. It
     * also shows how to save a variable to session
     * variable so that it is avialable after the postback
     * (page redraw from server)
     * caused by clicking a button
     */
    
    //class level variables
    int number1;
    int number2;
    int answer;

    protected void Page_Load(object sender, EventArgs e)
    {
        //this event method is used to do anything that needs to
        //be done before the page is loaded for the viewer
    }
    protected void btnGetAdditionProblem_Click(object sender, EventArgs e)
    {
        //call the addition method when the button is clicked
        Addition();
    }

    protected void GetRandom()
    {
        //get random numbers
        Random rand = new Random();
        number1 = rand.Next(0, 10);
        number2 = rand.Next(0, 10);

    }

    protected void Addition()
    {
        //get the random numbers
        GetRandom();
        //make the problem visible in a label
        lblProblem.Text = number1.ToString() + "+" + number2.ToString();
        //assign an answer to the problem
        answer = number1 + number2;
        //save the answer to a server side Session variable
        Session["CurrentAnswer"] = answer;
    }
    protected void btnCheckAnswer_Click(object sender, EventArgs e)
    {
        //get the user's answer from a text box
        int guess = int.Parse(txtAnswer.Text);
        //recall the answer from the server
        //the session variable stores everything as "object"
        //so you have to cast it back to the type of object
        //that it is--in this case an integer
        answer = (int)Session["CurrentAnswer"];
        //check to see if the answer is correct
        //and display the result
        if (guess == answer)
        {
            lblResult.Text = "Congrats";
        }
        else
        {
            lblResult.Text = "Sorry";
        }
    }
}

No comments:

Post a Comment