Weird problem :-(

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
S

Simon Harvey

Hi everyone

I am having the following very bizzare and annoying problem:

I have a page that contains amongst other things, a check box that is set
according to a session variable.

When the page loads, the check box is set according to the session variable.

Now here's the weird bit. If I change the checkbox and press the button that
I've also made, the checkbox.Checked property is still set to whatever it
was set to when the page loaded.

It's like its ignoring the fact that the checkbox was changed.

Does anyone know what could cause this?

Thanks all

Simon
 
is it loaded in the page_load?
If so only have it pull from the session when IsPostBack is false
 
What a dick I am. You're totally right.

Can't believe I missed that

Thanks for your help

Kindest Regards

Simon
 
Simon said:
I have a page that contains amongst other things, a check box that is set
according to a session variable.

When the page loads, the check box is set according to the session variable.

Now here's the weird bit. If I change the checkbox and press the button that
I've also made, the checkbox.Checked property is still set to whatever it
was set to when the page loaded.

It's like its ignoring the fact that the checkbox was changed.

It's likely caused because in your Page_Load event handler you're ALWAYS
setting the CheckBox' Checked property based on the Session variable
value, right? That is, you have something like:

Sub Page_Load(...)
myCB.Checked = Session("Blah")
End Sub

Instead, you should use:

Sub Page_Load(...)
If Not Page.IsPostBack then
myCB.Checked = Session("Blah")
End If
End Sub


Why? Well, the Page_Load event handler ALWAYS runs before ANY Web
control event handlers. So if you have, say, the CheckBox's
CheckChanged event wired up to some event handler, by the time that
runs, the CheckBox will have been reset back to the Session variable
value from the Page_Load event handler.

Does this make sense? I'm not 100% certain I'm directly addressing your
question... let me know if this doesn't clear things up... Thanks!


--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
Back
Top