Results 1 to 6 of 6

Thread: The strangest thing...

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    The strangest thing...

    Im building a medium sized app where I have on one page 3 dropdown boxes which I fill with data from a repeater

    I have a method which we can call "SetDpD" which fetch data from db and bind to dropdowns. This method is called ONCE, and that is on page_Load in the if not page.ispostback control statement

    I have a button which uses the selected items to show a result.

    Now the wierd thing, when the button_click event fires I check the selecteditem.value and it is the first item in the dropdown. It is like that every time I select some values and press the button.

    I thought I was insane... so I checked enableviewstate, and it was true, then I put a breakpoint on the SetDpD method and it was executed ONCE when the page was loaded, not any more.


    SO, my question is, what is it that can cause my dropdowns to loose their viewstate when I press the button? I don't manipulate the controls any more in the code unless upon page load


    kind regards
    henrik

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    I tried to make a new fillmethod where I filled the dropdowns with simple arraydata, and behold, postback worked like a charm... behold:


    VB Code:
    1. Public Sub FillDpdBoxes()
    2.         Dim con As New SqlConnection("Server=test.com;pwd=ytr;user id=hju;Database=driftstopp;")
    3.         con.Open()
    4.         Dim sqlcom As New SqlCommand
    5.         sqlcom.Connection = con
    6.         sqlcom.CommandText = "SELECT namn, attributtyp FROM dbo.attribut WHERE attributtyp = '83285585-B3DD-4B7A-86D8-65857683EA67' ORDER BY namn"
    7.         drpServer.DataSource = sqlcom.ExecuteReader(CommandBehavior.CloseConnection)
    8.         drpServer.DataTextField = "namn"
    9.         drpServer.DataValueField = "attributtyp"
    10.         drpServer.DataBind()
    11.        
    12.         con.Open()
    13.         sqlcom.CommandText = "SELECT namn, attributtyp FROM dbo.attribut WHERE attributtyp = '6E71059A-1B21-44E1-8C07-9522B46F90C3' ORDER BY namn"
    14.         drpAnsvarig.DataSource = sqlcom.ExecuteReader(CommandBehavior.CloseConnection)
    15.         drpAnsvarig.DataTextField = "namn"
    16.         drpAnsvarig.DataValueField = "attributtyp"
    17.         drpAnsvarig.DataBind()
    18.        
    19.  
    20. end sub

    And here is the method I replaced it with:

    VB Code:
    1. Public Sub FillDpdBoxes()
    2.         Dim server() As String = {"A", "B", "C", "D"}
    3.         Dim resp() As String = {"A", "B", "C", "D"}
    4.         drpServer.DataSource = server
    5.         drpAnsvarig.DataSource = resp
    6.         drpServer.DataBind()
    7.         drpAnsvarig.DataBind()
    8.        
    9.     End Sub


    Can someone please explain why the first piece of code screws up the viewstate of the two dropdowns but the second one doesn't?


    kind regards
    Henrik

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I'm not entirely sure what your problem is.

    If you databind a dropdown, and an item was selected on the client-side, then the dropdown will return the value that was selected after Page_Init. As long as you don't rebind the dropdown on postback, the dropdown contains the proper value.

    There are numerous things that can disrupt this model, and without lookin at your entire page code, would be difficult to ascertain.

    I would guess your first block of code (that didn't work), was binding to the wrong columns.

    So that's my short answer.

    Also, you should really assign the Command.ExecuteReader to a datareader, and bind the dropdown to the datareader, and then explicity close the datareader (as long as you using commandbehavior.closeconnection) with DataReader.Close().

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    My problem is very simple

    When I fill the dpdboxes with the first method, they revert to their original values upon postback


    And when I fill the dropdownboxes with the second function, they keep their values upon postback.

    I am pretty good with asp.net and I can't see why it is behaving like this. I have done the same thing many times before and have never caused a problem before. And when I trace the code upon postback the dropdown boxes aren't manipulated in any way... They just forget their values.

    Is there an obvious error in the first code that Im missing??

    kind regards
    HEnrik

  5. #5
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I just want to ask one question, sorry if it offends, but it has to be asked... are you assigning the values to boxes in a If Not IsPostBack block, so it doesn't rebind with the original every time the page posts back?

    Forgive me.. have to make sure.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Yeah of course

    I solved the problem... in a very ugly way...
    I took the values from the datareader and serialized them into an arraylist... then I bound the arraylist to the dropdowns... worked perfectly! No other changes had to be made...

    Either that or take the whole solution apart... which I had no time to do...

    I'll book this one as a .NET mystery

    kind regards
    Henrik

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width