adnan
adnan

Reputation: 1

ASP.NET and Posback?

When I use checkbox with a postback, my main class starts again at the first line. Therefore my variable changes again.

Like this:

public partial class Fırın1 : System.Web.UI.Page
{
    bool Checkboxlist1value, Checkboxlist1value = false;
    bool first_value=false;


    protected void Page_Load(object sender, EventArgs e)
    {


        Panel1.Visible = false;
        if (!Page.IsPostBack)
        {
            first_value = true;
        }
    }
}

As you can see in a small section of my code, when I click checkbox or save button class fırın_1 starts again at the first line. Thus first_value changes to false. But I need true because the condition will change this value to true.

How can I solve this problem?

Thanks in advance for your cooperation.

Upvotes: 0

Views: 83

Answers (1)

to StackOverflow
to StackOverflow

Reputation: 124766

If you want to persist values across a PostBack, you should generally store them in ViewState.

If I understand what you're trying to achieve, you should change first_value from a field to a property that's backed by ViewState. For example (I've changed it from first_value to FirstValue in line with Microsoft guidelines):

public bool FirstValue
{
    get
    {
        object o = ViewState["FirstValue"];
        if (o == null) return false; // default is false
        return (bool) o;
    }
    set
    {
        ViewState["FirstValue"] = value;
    }
}

...

protected void Page_Load(object sender, EventArgs e)
{


    Panel1.Visible = false;
    if (!Page.IsPostBack)
    {
        FirstValue = true;
    }
}

Upvotes: 1

Related Questions