JessiGirl
JessiGirl

Reputation: 43

How to prevent AutoPostBack from resetting my page?

I am building a page that includes some components that are static on the page (dropdowns, buttons, a table to hold them in), but one table cell is filled with variably generated CheckBoxes. When a button is pressed, the code for the page calculates what checkboxes to place and creates a new CheckBox object for each one needed and then adds it to an existing Div on the page.

I am trying to trigger some code to run when any of these are checked or unchecked, but checkBoxName.CheckedChanged += cbCheckedChanged wasn't working. I researched and found two suggestions: enabling viewstate and enabling autopostback.

checkBoxName.EnableViewState = true; seems to make no difference, nor did checkBoxName.ViewStateMode = ViewStateMode.Enabled; or any variations on that i tried. checkBoxName.AutoPostBack = true; did SOMETHING, but it's not allowing it to run the code I want. I think that's because it doesn't reach that point because of the next problem:

With AutoPostBack = true, whenever I check or uncheck a box, cbCheckedChangedis not being executed, and the entire page is reloading, resetting back to it's initial state, therefore removing the checkboxes completely from the table.

How can I fix these problems, or at least where might I start looking?

Edit:

This is where the checkboxes are created:

CheckBox cb = new CheckBox();
cb.Text = CBName;
cb.EnableViewState = true;
cb.ViewStateMode = ViewStateMode.Enabled;
cb.AutoPostBack = true;
cb.CheckedChanged += CBCheckedChanged;

and this is where CBCheckedChanged is:

private void CBCheckedChanged(object sender, EventArgs e)
{
\\Stuff
}

When I use breakpoints to step through it, it never reaches CBCheckedChanged. I have tried every possible combination of commenting out and leaving in the AutoPostBack, ViewStateMode, and EnableViewState lines.

Page_Load is currently empty, nothing runs until the user hits a button.

Upvotes: 4

Views: 3432

Answers (1)

Krisztián Balla
Krisztián Balla

Reputation: 20361

ViewState is by default enabled for server side controls. You don't have to tinker around with ViewState to solve your problem. ViewState is basically used to restore the state of a control after a postback happens. For example readding all entries to a ListBox control. This is why most control population code is within such a construct in the Page_Load method.

private void Page_Load()
{
    if (!Page.IsPostBack)
    {
        // populate controls here
    }
}

ViewState is a very misunderstood concept. There is a great article here that goes into it in detail. But as I said, for your problem ViewState is not a concern.

To solve your problem:

The problem in your case is that the triggered button is creating the CheckBox controls, but when the page is reloaded, because of the CheckedChanged event of these controls, the next page life cycle has no clue of the CheckBox controls that were placed on the page in the previous page life cycle. Dynamic controls need to be generated for every page life cycle!

So what I would do is create a method that:

  • creates the CheckBox controls and
  • sets AutoPostback = true for them and
  • sets the event handler for CheckedChanged

Let's call this method AddDynamicCheckBoxes(). Now you need to call this method in the Page_Load event of your Page when the button was already pressed and also in the event handler of the button's click event. You could do this like follows:

private void Page_Load()
{
    if (ViewState["button_was_clicked"] != null)
    {
        AddDynamicCheckBoxes();
    }
}

private void Button_OnClick()
{
    AddDynamicCheckBoxes();
    ViewState["button_was_clicked"] = true;
}

Upvotes: 3

Related Questions