p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

aspdotnet_website_programming thread: DropDownList not returning correct selected value


Message #1 by "J Donahue" <jdonahue@f...> on Fri, 10 May 2002 01:53:05
Hi.  I'm populating a dropdownlist from a table.  The user can only grant 
permissions equal to or below theres.  I populate the dropdownlist as 
follows:
---------------
strSQL = "SELECT PerNameVC,Rank FROM tPermissions Where Rank >= "
strSQL += GetUserRankString()
strSQL += " ORDER by Rank Desc"
Dim objDataView As DataView
objDataView = GetDataView(strConnect, strSQL)
If IsNothing(objDataView) Then
   error_exit()
End If
EmpPerm.DataSource = objDataView
EmpPerm.DataValueField = "Rank"
EmpPerm.DataTextField = "PerNameVC"
Page.DataBind()
--------------------
The code above populates my dropdownlist properly but when I execute the 
program, it doesn't matter what is selected...only the first item in the 
dropdownlist is ever returned.
I commented out the above and put it in as:
------------------------
EmpPerm.Items.Add("jim")
EmpPerm.Items.Add("testing1")
EmpPerm.Items.Add("testing2")
------------------------
That works and returns the correct item selected.  I've tried accessing 
the item selected as "EmpPerm.SelectedItem.Text" and "EmpPerm.Items
(EmpPerm.SelectedIndex).Value".  I'm assuming it must be due to my 
Page.DataBind() but I can't figure out why.  Any help would be 
appreciated.  Thanks!!!!
Message #2 by Feduke Cntr Charles R <FedukeCR@m...> on Fri, 10 May 2002 09:05:39 -0400
	I think you're probably experiencing a problem with ViewState.  If
ViewState is disabled for the entire page, enable for your drop down list
(EnableViewState="true").  If you disable for that particular control,
enable it.  My guess is you're trying to access the control *after* its been
repainted; try looking your your DropDownList_SelectedIndexChanged event, or
something like it.

	ViewState does suck for DropDownLists with 100+ entries, as the data
must be sent between client and server on every post.  There are ways to get
around it, but keep in mind you have to fire most events manually if you
take this route.

HTH,
- Chuck

-----Original Message-----
From: J Donahue [mailto:jdonahue@f...]
Sent: Thursday, May 09, 2002 9:53 PM
To: Website Programming with ASP.NET
Subject: [aspdotnet_website_programming] DropDownList not returning
correct selected value


Hi.  I'm populating a dropdownlist from a table.  The user can only grant 
permissions equal to or below theres.  I populate the dropdownlist as 
follows:
---------------
strSQL = "SELECT PerNameVC,Rank FROM tPermissions Where Rank >= "
strSQL += GetUserRankString()
strSQL += " ORDER by Rank Desc"
Dim objDataView As DataView
objDataView = GetDataView(strConnect, strSQL)
If IsNothing(objDataView) Then
   error_exit()
End If
EmpPerm.DataSource = objDataView
EmpPerm.DataValueField = "Rank"
EmpPerm.DataTextField = "PerNameVC"
Page.DataBind()
--------------------
The code above populates my dropdownlist properly but when I execute the 
program, it doesn't matter what is selected...only the first item in the 
dropdownlist is ever returned.
I commented out the above and put it in as:
------------------------
EmpPerm.Items.Add("jim")
EmpPerm.Items.Add("testing1")
EmpPerm.Items.Add("testing2")
------------------------
That works and returns the correct item selected.  I've tried accessing 
the item selected as "EmpPerm.SelectedItem.Text" and "EmpPerm.Items
(EmpPerm.SelectedIndex).Value".  I'm assuming it must be due to my 
Page.DataBind() but I can't figure out why.  Any help would be 
appreciated.  Thanks!!!!
Message #3 by "J Donahue" <jdonahue@f...> on Fri, 10 May 2002 16:29:27
Sheesh...the problem was staring me in the face.  I simply needed to put 
the code segment that I populated the dropdownlist within a "If Not 
Page.IsPostBack" statement.  Thanks for all your help!!!!

-------------------------------

Hi.  I'm populating a dropdownlist from a table.  The user can only grant 
permissions equal to or below theres.  I populate the dropdownlist as 
follows:
--------------
strSQL = "SELECT PerNameVC,Rank FROM tPermissions Where Rank >= "
strSQL += GetUserRankString()
strSQL += " ORDER by Rank Desc"
Dim objDataView As DataView
objDataView = GetDataView(strConnect, strSQL)
If IsNothing(objDataView) Then
   error_exit()
End If
EmpPerm.DataSource = objDataView
EmpPerm.DataValueField = "Rank"
EmpPerm.DataTextField = "PerNameVC"
Page.DataBind()
-------------------
The code above populates my dropdownlist properly but when I execute the 
program, it doesn't matter what is selected...only the first item in the 
dropdownlist is ever returned.
I commented out the above and put it in as:
-----------------------
EmpPerm.Items.Add("jim")
EmpPerm.Items.Add("testing1")
EmpPerm.Items.Add("testing2")
-----------------------
That works and returns the correct item selected.  I've tried accessing 
the item selected as "EmpPerm.SelectedItem.Text" and "EmpPerm.Items
(EmpPerm.SelectedIndex).Value".  I'm assuming it must be due to my 
Page.DataBind() but I can't figure out why.  Any help would be 
appreciated.  Thanks!!!!

  Return to Index