Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

c# local variables help

  • 31-05-2007 10: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