Setting Class-Level Variable to Use Between Event Handlers
Posted
by lush
on Stack Overflow
See other posts from Stack Overflow
or by lush
Published on 2010-03-17T16:42:24Z
Indexed on
2010/03/17
16:51 UTC
Read the original article
Hit count: 279
I'm having a hard time understanding why the following code doesn't work. I'm sure it's something remedial that I'm missing or not understanding. I currently have a page that asks for user input. If, based on the input and logged in user, I find data from this page already in the database, I need to update the existing records rather than creating new ones, so I set a class-level bool to true. The problem is, when MyNextButton is clicked, PreviouslySubmitted is still false. So, I'm not sure how to make the value of this variable persist. Any advice is appreciated, thanks.
public partial class MyForm : System.Web.UI.Page
{
private bool previouslySubmitted;
protected void Page_Load(object sender, EventArgs e)
{
MyButton.Click +=
(o, i) =>
{
q = from a in db.TableA
where (a.SomeField == SomeValue)
select a;
if(q.Any())
{
PreviouslySubmitted = true;
//populate the form's fields with values from database for user to revise
}
}
MyNextButton.Click +=
(o, i) =>
{
if(PreviouslySubmitted)
{
//update database
}
else
{
//insert into database
}
}
© Stack Overflow or respective owner