My ViewState is broken.

Discussion in 'ASP .Net' started by =?Utf-8?B?Q2hyaXMgR2lsYmVydA==?=, Sep 24, 2007.

  1. 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
    =?Utf-8?B?Q2hyaXMgR2lsYmVydA==?=, Sep 24, 2007
    #1
    1. Advertising

  2. "Chris Gilbert" <> wrote in message
    news:...

    > 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...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Sep 24, 2007
    #2
    1. Advertising

  3. =?Utf-8?B?Q2hyaXMgR2lsYmVydA==?=

    Roland Dick Guest

    Hi Chris,

    Chris Gilbert schrieb:
    > 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
    Roland Dick, Sep 24, 2007
    #3
  4. 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

    "Mark Rae [MVP]" wrote:

    > "Chris Gilbert" <> wrote in message
    > news:...
    >
    > > 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...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
    >
    =?Utf-8?B?Q2hyaXMgR2lsYmVydA==?=, Sep 24, 2007
    #4
  5. 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

    "Roland Dick" wrote:

    > Hi Chris,
    >
    > Chris Gilbert schrieb:
    > > 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
    >
    =?Utf-8?B?Q2hyaXMgR2lsYmVydA==?=, Sep 24, 2007
    #5
  6. "Chris Gilbert" <> wrote in message
    news:...

    > 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
    >> 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.


    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...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Sep 24, 2007
    #6
  7. Cool, thanks for the help Mark

    "Mark Rae [MVP]" wrote:

    > "Chris Gilbert" <> wrote in message
    > news:...
    >
    > > 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
    > >> 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.

    >
    > 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...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
    >
    =?Utf-8?B?Q2hyaXMgR2lsYmVydA==?=, Sep 24, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Rush
    Replies:
    2
    Views:
    499
    Ben Rush
    Dec 5, 2003
  2. Steph
    Replies:
    2
    Views:
    1,642
    heislord5
    May 11, 2011
  3. Steven D'Aprano

    Why are "broken iterators" broken?

    Steven D'Aprano, Sep 21, 2008, in forum: Python
    Replies:
    8
    Views:
    632
  4. Cameron Simpson

    Re: Why are "broken iterators" broken?

    Cameron Simpson, Sep 22, 2008, in forum: Python
    Replies:
    0
    Views:
    563
    Cameron Simpson
    Sep 22, 2008
  5. Fredrik Lundh

    Re: Why are "broken iterators" broken?

    Fredrik Lundh, Sep 22, 2008, in forum: Python
    Replies:
    0
    Views:
    585
    Fredrik Lundh
    Sep 22, 2008
Loading...

Share This Page