My ViewState is broken.

G

Guest

I am unable to retrieve the values of DropDownLists upon PostBack. Before
you ask, yes I am using Page_Load to handle the PostBack and no I'm not
overwriting the old values by repopulating the DDL's.

I had a page with some quite complex code but soon found that when I tried
to read the values posted back from DDL's they were all empty strings.
ASP.Net textboxes are fine and I am able to retreive those values but not the
DDL's. I cut back the code trying to resolve the problem until I eventually
decided to try the simplest example possible to see if it worked. I should
point out that if the DDL is hard coded i.e. not populated with a databind it
works fine.

Here's the aspx page:

<form id="ddlForm" runat="server">

<asp:DropDownList ID="ddl" runat="server"></asp:DropDownList>

<input type="submit" value="submit" />

<p id="ddlValue" style="color:Red;" runat="server"></p>

</form> and here's the code behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

If Not IsPostBack Then
populateDDL(ddl)
Else 'UNABLE TO RETREIVE VALUE HERE
ddlValue.InnerHtml = ddl.SelectedValue
End If

End Sub

Private Sub populateDDL(ByVal ddl As DropDownList)

Dim counter As Integer

For counter = 0 To 12
ddl.Items.Add(New ListItem("Item" & counter.ToString))
Next

End Sub

All pretty simple as you can see and as you would expect it works perfectly
when I try it on my home computer. If I try it on my PC at work however I am
unable to get the values of the DDL where commented. I can only assume that
there is something wrong with my .Net install or something similar.

I have tried all sorts of things like the text property, value property etc.
In fact the ddl.selectedItem is Nothing. When the page reloads, my DDL has
nothing in it on the page i.e it hasn't kept its state whatsoever. When i try
it on the working machine the <p> tag gets the correct value i.e. Item0 or
Item4 etc and the DDL has all the values in it with the value i choose still
selected as you would expect. The ViewState hidden field has an expectedly
large encrypted value in it. On the broken machine however, the ViewState is
only about 30 charcters long and the DDL has no values and as such the <p>
has no value. Interestingly though I can retreive the value through
Request.Form.

I've come to the conclusion that there is something wrong with my ViewState
perhaps. Clearly this is a major problem. Any idea on how to fix it would be
appreciated. I've compared my Machine.Config and Web.Config from a working
machine to the one on the problem machine and they are the same.

Any idea as to the problem?

Thanks in advance,

Chris
 
M

Mark Rae [MVP]

Before you ask, yes I am using Page_Load to handle the PostBack

What do you actually mean by that...? Page_Load doesn't "handle postbacks" -
Page_Load is an event which fires when a page loads...

Anyway, if you move the initial population of the DropDownList controls out
of Page_Load and into Page_Init, everything should be fine...
 
R

Roland Dick

Hi Chris,

Chris said:
Private Sub populateDDL(ByVal ddl As DropDownList)

Dim counter As Integer

For counter = 0 To 12
ddl.Items.Add(New ListItem("Item" & counter.ToString))
Next

End Sub

not sure whether that's your problem, but your listitem doesn't have a
value set. Try the constructor with two parameters.

Also, look at the source in the browser; does ASP.NET spit out the HTML
you would expect i.e. with value-tags?

Just a longshot, but I hope it helps,

Roland
 
G

Guest

Sorry let me explain.

Whenever I (or others) ask this question in forums the first thing people
say is:

"If you are populating your DropDownList in Page_Load then you might be
overwriting your values upon postback."

I'm just trying to point out that I am aware of this possibility and this is
not the case.

Anyway, if you move the initial population of the DropDownList controls out
of Page_Load and into Page_Init, everything should be fine...

Why is this. You're right it has solved my problem but I've never had to do
this before, why now? Surely if I call it on page init the values should be
overwritten??? Thanks for the help if you could just take 5 seconds to
explain why I'd apprectiate it.

Thanks again

Chris
 
G

Guest

Sorry should have pointed out that I can't retreive the value, text or
anything. The example, when tested on my PC at home works fine i.e. I can
illustrate that the DDL is repopulated and I can see the selected item. On
my development PC however I can't.

Mark Rae has suggested that I put the population of the DDL in Page_Init and
it seems to have done the job. Not sure why as I've never had to do this
before and like I said, I works on my other PC??!!

Thanks,

Chris
 
M

Mark Rae [MVP]

I'm just trying to point out that I am aware of this possibility and this
is
not the case.

S'OK - I understood...
Why is this. You're right it has solved my problem but I've never had to
do
this before, why now? Surely if I call it on page init the values should
be
overwritten??? Thanks for the help if you could just take 5 seconds to
explain why I'd apprectiate it.

Page life cycle: http://msdn2.microsoft.com/en-us/library/ms178472.aspx

Page_Load is just one of the events that happen when a page loads. However,
these events happen in a specific order, and several things occur in each of
the events (usually)...

One of the events that happens during Page_Load is that, for a postback),
control properties are loaded from ViewState and control state. If you also
write code to populate those controls in the Page_Load event, there's no
guarantee that your code will run before the ViewState is reapplied.
Sometimes it does, sometimes it doesn't...

Populate your controls in Page_Init, however, and you will never encounter
this problem...
 
G

Guest

Cool, thanks for the help Mark

Mark Rae said:
S'OK - I understood...


Page life cycle: http://msdn2.microsoft.com/en-us/library/ms178472.aspx

Page_Load is just one of the events that happen when a page loads. However,
these events happen in a specific order, and several things occur in each of
the events (usually)...

One of the events that happens during Page_Load is that, for a postback),
control properties are loaded from ViewState and control state. If you also
write code to populate those controls in the Page_Load event, there's no
guarantee that your code will run before the ViewState is reapplied.
Sometimes it does, sometimes it doesn't...

Populate your controls in Page_Init, however, and you will never encounter
this problem...
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top