How does the viewstate work?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

See below for example code.

My problem is that when the drop down list posts back the event handler for
the check box also executed. I have no idea why this happens?

The only odd thing I can see is [using the trace directive] the viewstate
size bytes of the check box is 0 but after a post back it is non zero.

Any help and advice most welcome!

<!-- Begin ASP.NET Code -->
......
<asp:dropdownlist runat="server" id="lstFontProperties" autopostback="True" />
<asp:checkbox runat="server" id="chkLegend" text="Show Legend"
autopostback="True" />
<asp:textbox runat="server" id="txtLegendTitle" visible="false" />
......
<!-- End ASP.NET Code -->

My page uses code behind and both the drop down list and the check box have
an event handler associated with it self. The textbox only appears when the
check box is checked (txtLegendTitle.Visible = chkLegend.Checked).
 
We've copied your code and if we wire up event handlers to the dropdownlist
and checkbox the event handlers are executed correctly, only one of them at a
time. The fact that the viewstate is 0 at the beginning and later becomes non
zero is insignificant. After the first postback it contains 36 bytes (in our
case) containing the state of the checkbox (either 'on' or 'off'). This
viewstate size doesn't change when the checkbox becomes checked. The one
thing that differs between a checkbox which is checked and one which isn't is
the render size. Maybe you should try again on a new page, as the asp.net
code you've posted is correct. Hth.

Kind regards,
Nikander & Margriet Bruggeman
 
Have you accidentally wired up both controls to the same event?

Have a look in the code behind method InitializeComponent

MattC
 
I've had a look at the InitializeComponent and no they haven't been wired up
to the same control.

After having stepped through the code I see the list event handler being
fired then immediately after the event handler for the checkbox executes.

MattC said:
Have you accidentally wired up both controls to the same event?

Have a look in the code behind method InitializeComponent

MattC

Naeem Sarfraz said:
See below for example code.

My problem is that when the drop down list posts back the event handler
for
the check box also executed. I have no idea why this happens?

The only odd thing I can see is [using the trace directive] the viewstate
size bytes of the check box is 0 but after a post back it is non zero.

Any help and advice most welcome!

<!-- Begin ASP.NET Code -->
.....
<asp:dropdownlist runat="server" id="lstFontProperties"
autopostback="True" />
<asp:checkbox runat="server" id="chkLegend" text="Show Legend"
autopostback="True" />
<asp:textbox runat="server" id="txtLegendTitle" visible="false" />
.....
<!-- End ASP.NET Code -->

My page uses code behind and both the drop down list and the check box
have
an event handler associated with it self. The textbox only appears when
the
check box is checked (txtLegendTitle.Visible = chkLegend.Checked).
 

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

Back
Top