tags:

views:

291

answers:

6

Hello,

I have a boolean variable declared at the top of a class and when a radio button is selected on a page, the variable gets set to true, but when the page is reloaded, the variable gets reset back to false. One way I have handled this was by using the static keyword, but I am not sure if this is the best way to handle this. Here is the class where I tried doing things in the Page_Load event, but it is still resets the variables to false.

public class SendEmail
{
    bool AllSelected;

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        AllSelected = false;
    }
}

protected void rbAll_SelectedIndexChanged(object sender, EventArgs e)
{
    if(rbAll.SelectedValue == "All")
       AllSelected = true;
}

public Send()
{
   if(AllSelected)
   {
       //Send Email. Never runs because AllSelected is always false;
   }
}

}
+10  A: 

When the page gets reloaded, a new instance of your page class is created so any values from the last server interaction are lost. Put the value into viewstate if you want it to persist across postbacks:

bool AllSelected
{
    get
    {
        object o = ViewState["AllSelected"];
        if(o == null) return false;
        return (bool)o;
    }
    set
    {
        ViewState["AllSelected"] = value;
    }
}

The ViewState collection is written in a hidden element into the form in the client's browser, and posted back and restored the next time they click a button or do any other "postback" type action.

Barry Fandango
Can I set the boolean variables to static as well?
Xaisoft
If you make it static, it will only have one value across all requests, including when multiple people are accessing the page. So that would get pretty ugly.
Barry Fandango
ok, i guess i need to read up on static variables, they are kind of confusing to me still, lol. Thanks.
Xaisoft
To be more specific Xiasoft, your static variable would have one value per ASP.NET worker process. Generally, if multiple people were using the page at once (a real possibility with web pages) they would all be reading and writing values to that same variable, on top of each other.
Barry Fandango
+1  A: 

Your boolean is an instance variable, so it will get the default value (which is false for bools) every time you create a new instance of your class.

Brian Rasmussen
+2  A: 

Every time asp.net serves a page, it creates a new instance of the page class. This means that AllSelected will always be auto initialized to false.

My suggestion, unless there is something I don't see here, is to just call Send() from your SelectedIndexChanged method.

Max Schmeling
There is more to the class, I just cut it out.
Xaisoft
+1  A: 

Remember, every request to your page uses a brand new instance of your page class. This includes postbacks.

Joel Coehoorn
+1  A: 

You need your variable to be stored. I'd suggest storing it in ViewState or if you want to stay away from ViewState, hide it in a form element on the page.

Also, I'm not seeing where Send is being called.

Jason N. Gaylord
What about declaring them as static? Does that cause any problems?
Xaisoft
+1  A: 

Not to jump down on you or anything...but why not just check if

rbAll.SelectedValue == "All"

in your send function?

No storage...no populating the ViewState or Session with data that isn't needed...

Jason Punyon
I know. There is a little more to it. I cut a lot of code out to make it short.
Xaisoft