Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 15th, 2012, 12:39 PM
Authorized User
Points: 56, Level: 1
Points: 56, Level: 1 Points: 56, Level: 1 Points: 56, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2010
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
Default DropDownList.SelectedValue - NOT working

Helo
I have databinded some values from a Sql 2005 database table into a dropdown list.
My problem is in trying to get the selected value from the dropdown list so I can use it.
What is happening is that I am only getting the selected index of the selected value in the dropdown list. But I want to get the selected value NOT the selected index. My codes are as below:

<asp:DropDownList ID="ddlCountry" runat="server" Width="255px" TabIndex="11"
DataSourceID="ds_Countries" DataTextField="CountryName"
DataValueField="CountryId" AppendDataBoundItems="True" AutoPostBack="True">
<asp:ListItem>Please Select Country</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="ds_Countries" runat="server"
ConnectionString="<%$ ConnectionStrings:MyPeopleConnectionString1 %>"
SelectCommand="SELECT [CountryId], [CountryName] FROM [Countries] ORDER BY [CountryName]">
</asp:SqlDataSource>

Private Function CollectUserContactDetails() As UserContactDetail

Dim ucd As New UserContactDetail


ucd.Postcode = Me.txtPostcode.Text.Trim()

ucd.Country = Me.ddlCountry.SelectedValue ‘Not working

ucd.HomeTelephoneNumber = Me.txtHomeTelephone.Text
ucd.WorkTelephoneNumber = Me.txtWorkTelephone.Text
ucd.MobileNumber = Me.txtMobileNumber.Text

Return ucd

End Function
Any help will be highly appreciated.

Thanks
Sharif
  #2 (permalink)  
Old February 15th, 2012, 12:44 PM
Imar's Avatar
Wrox Author
Points: 72,022, Level: 100
Points: 72,022, Level: 100 Points: 72,022, Level: 100 Points: 72,022, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,076
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

As suggested in my previous post, there is no reason why this doesn't work so you'll need to provide additonal code and background if you want us to understand what's going on.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #3 (permalink)  
Old February 16th, 2012, 01:04 AM
Friend of Wrox
Points: 742, Level: 10
Points: 742, Level: 10 Points: 742, Level: 10 Points: 742, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2009
Location: Tehran, Iran
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Smile

Hello there
View Source of generated page and analyse tags:

<select id="x$ddlCountery" ...>
<option>Please Select Counter</option>
<option value="1">Country1</option>
<option value="2">Country2</option>
<option value="3">Country3</option>
</select>

see the 'value' of option tags
__________________
happy every time, happy every where

Reza Baiat
  #4 (permalink)  
Old February 19th, 2012, 08:31 AM
Authorized User
Points: 56, Level: 1
Points: 56, Level: 1 Points: 56, Level: 1 Points: 56, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2010
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
Default

No, it’s still not fixed yet Imar. Let me give little bit more information about this problem.
In one of my pages, SignUpDetail.aspx, I’ve added a DropDownList control called ‘ddlCounty’, which is using a connection string called ‘MyPeopleConnectionString1’ to connect to a SQL 2005 Database table called ‘dbo.Countries’. At run-time the ‘ddlCountry’ displays all the country names as expected. Now, in the page’s code behind file, I’m trying to read the selected country name and assigned it to a string variable. BUT the variable is always getting the Index value of the selected country, NOT the actual name of the country. Part of my code is as below:
[]
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

_uName = CollectUserName()
_uDetail = CollectUserContactDetails()

If (_uBLL.InsertUserInformation(_uName, _uDetail)) = True Then
Response.Write("Insertion Successful")
End If

End Sub

Private Function CollectUserName() As UserName

Dim un As New UserName

un.UserId = _strUserId
un.Title = Me.ddlTitle.Text
un.FirstName = Me.txtFirstName.Text.Trim()
un.MiddleName = Me.txtMiddleName.Text.Trim()
un.Surname = Me.txtSurname.Text.Trim()
un.Nickname = Me.txtNickname.Text.Trim()
un.Gender = Me.ddlGender.Text

Return un

End Function

Private Function CollectUserContactDetails() As UserContactDetail

Dim ucd As New UserContactDetail

ucd.UserId = _strUserId
ucd.HouseNameOrNumber = Me.txtHouseNumber.Text.Trim()
ucd.Street = Me.txtStreet.Text.Trim()
ucd.Town = Me.txtTown.Text.Trim()
ucd.County = Me.txtCounty.Text.Trim()
ucd.Postcode = Me.txtPostcode.Text.Trim()
ucd.Country = Me.ddlCountry.SelectedValue ' NOT WORKING
ucd.HomeTelephoneNumber = Me.txtHomeTelephone.Text
ucd.WorkTelephoneNumber = Me.txtWorkTelephone.Text
ucd.MobileNumber = Me.txtMobileNumber.Text

Return ucd
End Function
[/]
Hope this will give you bit clearer picture of my problem.
Thanks
Sharif
  #5 (permalink)  
Old February 19th, 2012, 01:43 PM
Friend of Wrox
Points: 742, Level: 10
Points: 742, Level: 10 Points: 742, Level: 10 Points: 742, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2009
Location: Tehran, Iran
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Smile it works

Hi there

I tried what you mentioned and there was no problem with selectedValue (as it should be). I guess that your CountryId's and SelectedIndex are similar and make you confused! try below code for test reasons:

HTML Code:
<asp:DropDownList ID="ddlCountry" runat="server" Width="255px" TabIndex="11"
DataSourceID="ds_Countries" 

DataTextField="CountryId"
DataValueField="CountryName" 

AppendDataBoundItems="True" AutoPostBack="True">
<asp:ListItem>Please Select Country</asp:ListItem>
</asp:DropDownList>
__________________
happy every time, happy every where

Reza Baiat
  #6 (permalink)  
Old February 20th, 2012, 07:31 AM
Authorized User
Points: 56, Level: 1
Points: 56, Level: 1 Points: 56, Level: 1 Points: 56, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2010
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Hi Reza
Many thanks for your response to my problem.
I use your suggested code. Now I am having another problem, the countries DropDownList, 'ddlCountry', is now only displaying Indexes, NO names for the countries. However, it returns the country name at the code behind file, which was my initial problem. So, to fix the both problem, I, myself, amended the code like below:
[
<asp:DropDownList ID="ddlCountry" runat="server" Width="255px" TabIndex="11"
DataSourceID="ds_Countries"
DataTextField="CountryName"
AppendDataBoundItems="True">
<asp:ListItem>Please Select Country</asp:ListItem>
</asp:DropDownList>
/]

Now it's working fine. I'm happy. But whether this is the right thing to do, I'm not sure.
Anyway, thank you again Reza for providing me some important hints to solve my problem.
Wish you all the best for your programming career.
Sharif
  #7 (permalink)  
Old February 20th, 2012, 10:17 PM
Imar's Avatar
Wrox Author
Points: 72,022, Level: 100
Points: 72,022, Level: 100 Points: 72,022, Level: 100 Points: 72,022, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,076
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

I think you're confused about what the "index" is. The index in a drop down is the zero based sequential number of the item in the list. E.g., when rendered into this HTML:

<option value="6">Holland</option>
<option value="1">USA</option>

the index (accessible through the SelectedIndex of the DropDownList at the server) of the Holland item would be 0, and 1 for the US item. The value (SelectedValue) for these items would be 6 and 1 respectively.

After reading this entire thread, I think when you said "I get the index back from SelectedValue", my guess is that you meant you get back the value but wanted the name. E.g. for Holland you got back 6, but wanted Holland. However, by using the terms you were using, we thought you got back 0, but wanted 6.

Does that make sense? And is that indeed the case?

If so, what you want is the Text property of the SelectedItem. That would return Holland. So, ddlCountries.SelectedItem.Text.

The suggested change by irProject was not meant as a final change in your project; just some temporary change in your code that would help you discover this.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; February 20th, 2012 at 10:29 PM..
The Following User Says Thank You to Imar For This Useful Post:
shariful (February 21st, 2012)
  #8 (permalink)  
Old February 21st, 2012, 06:47 AM
Authorized User
Points: 56, Level: 1
Points: 56, Level: 1 Points: 56, Level: 1 Points: 56, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2010
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Hi Imar
Many thanks for your valuable suggestions. You're absolutely right, I was not clear about the uses of the SelectedIndex, SelectedItem and SelectedValue. It's now clear to me. I've amended the code according to your suggestion and it's working fine. Fantastic.
Thanks again.
Sharif
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dropdownlist.selectedvalue is NOT working shariful BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 February 16th, 2012 11:26 AM
Need Help with DropDownList SelectedValue putputty ASP.NET 2.0 Basics 4 June 27th, 2007 11:44 AM
Unable to set SelectedValue on DropDownList jlrolin ASP.NET 2.0 Professional 0 April 13th, 2007 09:30 AM
DropDownList.SelectedValue weirdness Aaron Edwards ASP.NET 2.0 Basics 5 March 7th, 2007 12:39 AM
Assigning a SelectedValue in a DropDownList rit01 ASP.NET 2.0 Professional 3 March 25th, 2006 01:12 AM



All times are GMT -4. The time now is 06:48 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.