ViewState chaos

G

Guest

Following problem:

I have on the page one (graphic-)button (its click even is wired), some
other stuff and of course the page_load function

Page_Load function has the following entry:

{
if (!Page.IsPostBack)
{
counttextboxes = 0;
ViewState.Add("counttextboxes", counttextboxes);

}

counttextboxes = (int) ViewState["counttextboxes"];


Label1.Text = counttextboxes.ToString();

}



ImageButton1_Click has:

{

counttextboxes = (int) ViewState["counttextboxes"];
counttextboxes++;
ViewState.Add("counttextboxes", counttextboxes);


Label2.Text = Convert.ToString(counttextboxes);

}


as you guessed, the problem is counttextboxes (an int, declared as protected
in the class).

If I click on the first button, thus activating button1_click,
counttextboxes gets indeed bigger. But on page_load (when I test it) it is
always one number smaller than it's on Imagebutton1_click.


If Label1 displays 5, label2 displays 6.


How to even them up? _Without_ this method on page load by the way:

if (Convert.ToBoolean(counttextboxes))
counttextboxes = counttextboxes + 1;

Thank you for your time.
 
M

Mythran

"the friendly display name"
Following problem:

I have on the page one (graphic-)button (its click even is wired), some
other stuff and of course the page_load function

Page_Load executes before control events execute. So, you are displaying
the value of the counttextboxes value before you are incrementing it in the
click event.

Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top