 |
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

February 15th, 2012, 12:39 PM
|
Authorized User
|
|
Join Date: Nov 2010
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
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
|

February 15th, 2012, 12:44 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|

February 16th, 2012, 01:04 AM
|
Friend of Wrox
|
|
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
|
|
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
|

February 19th, 2012, 08:31 AM
|
Authorized User
|
|
Join Date: Nov 2010
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
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
|

February 19th, 2012, 01:43 PM
|
Friend of Wrox
|
|
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
|
|
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
|

February 20th, 2012, 07:31 AM
|
Authorized User
|
|
Join Date: Nov 2010
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
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
|

February 20th, 2012, 10:17 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
Last edited by Imar; February 20th, 2012 at 10:29 PM..
|
The Following User Says Thank You to Imar For This Useful Post:
|
|

February 21st, 2012, 06:47 AM
|
Authorized User
|
|
Join Date: Nov 2010
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |