Hi there- I'm currently learning C# and have a question about ViewState.
I have a simple Account Balance Calculator that uses ViewState to store the current Balance for the next calculation. The Balance is determined via selecting either a Deposit, Withdraw or Service radio button. Code behind below:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace Bank_Acount_Calculator
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//initalize ViewState
if (ViewState["NewBalance"] == null)
ViewState["NewBalance"] = 0.0;
//check Deposit
rBtn_Deposit.Checked = true;
}
}
protected void btn_Transaction_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
double fee = 10.00;
double balance = (double)ViewState["NewBalance"];
//set the string value of EnterAmount TextBox to double
double txtBox_Total = Convert.ToDouble(tb_EnterAmount.Text);
//type conditional
if (rBtn_Deposit.Checked == true)
{
balance += txtBox_Total;
}
else if (rBtn_Withdraw.Checked == true)
{
if (balance < txtBox_Total)
{
balance -= fee;
}
else
{
balance -= txtBox_Total;
}
}
else
{
balance -= txtBox_Total;
}
//assign the value
ViewState["NewBalance"] = balance;
//display value
lbl_NewBalance.Text = balance.ToString("c");
}
}
}
}
While this works well, the project is asking that i write custom classes to generate the Depost, Withdraw and Service amounts. When I attempt to create a class, and referencing ViewState Object within, I receive the "The name 'ViewState' does not exist in the current context" error. Advice is totally appreciated! Thanks!