p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Basics (http://p2p.wrox.com/forumdisplay.php?f=351)
-   -   DropDownList.SelectedValue - NOT working (http://p2p.wrox.com/showthread.php?t=86683)

shariful February 15th, 2012 12:39 PM

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

Imar February 15th, 2012 12:44 PM

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

irProject February 16th, 2012 01:04 AM

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

shariful February 19th, 2012 08:31 AM

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

irProject February 19th, 2012 01:43 PM

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>


shariful February 20th, 2012 07:31 AM

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

Imar February 20th, 2012 10:17 PM

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

shariful February 21st, 2012 06:47 AM

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


All times are GMT -4. The time now is 10:24 PM.

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