Xaisoft
Xaisoft

Reputation: 46591

Why is the boolean variable always resetting to false;

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;
   }
}

}   

Upvotes: 3

Views: 7388

Answers (7)

Emre
Emre

Reputation: 54

I don't really know if this is going to be handy in asp.net but I create a new bool in property.settings so that it remembers the bool whenever I close or restart the application. But I think this is more for winforms.

Upvotes: 0

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...

Upvotes: 1

Jason N. Gaylord
Jason N. Gaylord

Reputation: 8324

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.

Upvotes: 2

Joel Coehoorn
Joel Coehoorn

Reputation: 415810

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

Upvotes: 1

Max Schmeling
Max Schmeling

Reputation: 12509

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.

Upvotes: 2

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

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.

Upvotes: 1

James Orr
James Orr

Reputation: 5135

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.

Upvotes: 15

Related Questions