Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C#
Hello all,
How can I store the value of the variables which are under Button_Click event, so that I can use that value again for another Button_Click event ?
Posted 22-Nov-12 3:18am
Anele Ngqandu at 22-Nov-12 8:21am
Have you tried sessions?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

   // one button click
Session["op1"] = op1;
Session["op2"] = op2;
Session["op"] = op;
//another button click event
op1 = int.Parse(Session["op1"].ToString());
            op2 = int.Parse(Session["op2"].ToString());
            op = Session["op"].ToString();
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

That depends on the environment you are in.
The procedure for WinForms is very different from the procedure for a web site.
For WinForms, all you need to do is move teh definition to class level, so it is not within the click event handler:
private int myInt = 6;
private void myButton_Click(object sender, EventArgs e)
For Web, it is a bit more complex, because class variables are not maintained after a page is loaded to the client (and they will all be reset before a post back). In this case, use the Session:
   int myInt = Session["MyInt"];
   Session["MyInt"] = myInt;
Krunal R at 22-Nov-12 8:31am
Thanks mate, but I have solved this myself :)
Madhuri Patil at 22-Nov-12 9:01am
If you solved it then just accept solution/or add your solution and accept it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 535
1 OriginalGriff 275
2 Shweta N Mishra 244
3 BillWoodruff 235
4 Deepu S Nair 230
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,818
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,755

Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100