Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

My ViewState is broken.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris Gilbert - 24 Sep 2007 10:06 GMT
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
Mark Rae [MVP] - 24 Sep 2007 10:13 GMT
> 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...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Chris Gilbert - 24 Sep 2007 10:44 GMT
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

> > Before you ask, yes I am using Page_Load to handle the PostBack
>
[quoted text clipped - 3 lines]
> Anyway, if you move the initial population of the DropDownList controls out
> of Page_Load and into Page_Init, everything should be fine...
Mark Rae [MVP] - 24 Sep 2007 13:28 GMT
> I'm just trying to point out that I am aware of this possibility and this
> is
> not the case.

S'OK - I understood...

>> Anyway, if you move the initial population of the DropDownList controls
>> out
[quoted text clipped - 6 lines]
> 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...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Chris Gilbert - 24 Sep 2007 15:46 GMT
Cool, thanks for the help Mark

> > I'm just trying to point out that I am aware of this possibility and this
> > is
[quoted text clipped - 27 lines]
> Populate your controls in Page_Init, however, and you will never encounter
> this problem...
Roland Dick - 24 Sep 2007 10:16 GMT
Hi Chris,

Chris Gilbert schrieb:
> Private Sub populateDDL(ByVal ddl As DropDownList)
>
[quoted text clipped - 5 lines]
>
> 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
Chris Gilbert - 24 Sep 2007 10:46 GMT
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

> Hi Chris,
>
[quoted text clipped - 18 lines]
>
> Roland

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.