Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

c# local variables help

  • 31-05-2007 11:38AM
    #1
    Registered Users, Registered Users 2 Posts: 872 ✭✭✭


    Hi,

    In my code behind page i have

    private int propertyDescription = 1;

    which is defined just above the page load (under all the web controls)

    when someone checks a box i set propertyDescription = 2 in the event handler but the value isnt getting updated. Any ideas ?

    I know i should know how to do this !!

    Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Sounds like a state issue. Http is stateless so your site won't maintain values between the client and the server for you. You have to store them somewhere yourself. Put the value into ViewState or the Session to maintain it between the postbacks.

    Try
    protected void Page_Load(object sender, EventArgs e)
    {
       if(!IsPostBack)
       {
            ViewState["_propertyDescription"] = 1;
        }
    }
    // Your event handler goes here
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        ViewState["_propertyDescription"] = 2;
    }
    

    Then to access the propertyDescription value
    private void foo()
    {
        // Assumes you want it as an Int32
        Int32 propDescription = Convert.ToInt32(ViewState["_propertyDescription]); 
    }
    


Advertisement