locked
ViewState + Custom class RRS feed

  • Question

  • 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!

    • Moved by Rudedog2 Tuesday, November 24, 2009 5:13 PM asp.net is off-topic (From:Visual C# General)
    Tuesday, November 24, 2009 4:55 PM

Answers

  • One option would be to pass the ViewState reference to your custom object, otherwise you might be able to get to it through the HttpContext objcet.
    • Marked as answer by workhardened Wednesday, November 25, 2009 2:15 PM
    Tuesday, November 24, 2009 5:07 PM

All replies

  • One option would be to pass the ViewState reference to your custom object, otherwise you might be able to get to it through the HttpContext objcet.
    • Marked as answer by workhardened Wednesday, November 25, 2009 2:15 PM
    Tuesday, November 24, 2009 5:07 PM
  • Your best option would be to post on the ASP.NET forum .

    The web heads hang out there. 
    Only a few visit this forum.
    You should get better answers.
    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by Chao Kuo Saturday, November 28, 2009 6:48 AM
    Tuesday, November 24, 2009 5:12 PM