|
-
Mar 1st, 2004, 09:04 AM
#1
Thread Starter
Frenzied Member
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
-
Mar 1st, 2004, 09:29 AM
#2
Thread Starter
Frenzied Member
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:
Public Sub FillDpdBoxes()
Dim con As New SqlConnection("Server=test.com;pwd=ytr;user id=hju;Database=driftstopp;")
con.Open()
Dim sqlcom As New SqlCommand
sqlcom.Connection = con
sqlcom.CommandText = "SELECT namn, attributtyp FROM dbo.attribut WHERE attributtyp = '83285585-B3DD-4B7A-86D8-65857683EA67' ORDER BY namn"
drpServer.DataSource = sqlcom.ExecuteReader(CommandBehavior.CloseConnection)
drpServer.DataTextField = "namn"
drpServer.DataValueField = "attributtyp"
drpServer.DataBind()
con.Open()
sqlcom.CommandText = "SELECT namn, attributtyp FROM dbo.attribut WHERE attributtyp = '6E71059A-1B21-44E1-8C07-9522B46F90C3' ORDER BY namn"
drpAnsvarig.DataSource = sqlcom.ExecuteReader(CommandBehavior.CloseConnection)
drpAnsvarig.DataTextField = "namn"
drpAnsvarig.DataValueField = "attributtyp"
drpAnsvarig.DataBind()
end sub
And here is the method I replaced it with:
VB Code:
Public Sub FillDpdBoxes()
Dim server() As String = {"A", "B", "C", "D"}
Dim resp() As String = {"A", "B", "C", "D"}
drpServer.DataSource = server
drpAnsvarig.DataSource = resp
drpServer.DataBind()
drpAnsvarig.DataBind()
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
-
Mar 1st, 2004, 11:08 AM
#3
I wonder how many charact
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().
-
Mar 2nd, 2004, 02:00 AM
#4
Thread Starter
Frenzied Member
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
-
Mar 6th, 2004, 02:25 PM
#5
I wonder how many charact
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.
-
Mar 8th, 2004, 03:03 AM
#6
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|