Drop down list text value not posting back with correct text?

Hello:

This may sound confusing, but I have dropdownlist and databinding it, and have set the DataTextField and DataValueField correctly, but on the page itself when I choose the text from the dropdown it acts wacky on some of the values and chooses another value.

here is some code:

aspx page

<asp:dropDownList ID="outside_assoc_cities" runat="server" AutoPostBack="True" Enabled="False" DataTextField="city" DataValueField="mls_id" Visible="False"></asp:dropDownList>

codebehind

Protected Sub recip_form_type_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles recip_form_type.SelectedIndexChanged
If recip_form_type.SelectedItem.Value <> "0" Then
outside_assoc_cities.Enabled = True
ElseIf recip_form_type.SelectedItem.Value = "0" Then
outside_assoc_cities.Enabled = False
outside_assoc_cities.SelectedIndex = "0"
End If
End Sub

If anyone wants to see this wackiness, let me know so I can put it on a public site.

 

0
mebinici
12/13/2006 12:39:53 AM
asp.net.web-forms 93655 articles. 5 followers. Follow

12 Replies
884 Views

Similar Articles

[PageSpeed] 36

There is nothing wacky if you are setting multiple Items in the list with the same value "value". This is how the HTML Form Element is by design and has nothing to do with the Server Control. Remeber that the server controls are reinstantiated by the standard HTML Form that is posted back to the server.

Would this be the problem?


Rgds,
Martin.

For the benefit of all users please mark any post answers as appropriate.
0
mokeefe
12/13/2006 5:53:25 AM
mokeefe:

This is how the HTML Form Element is by design ...

By this I mean the child SELECT element that is generated by the server side DropDownList Control - not the actual Form Element.


Rgds,
Martin.

For the benefit of all users please mark any post answers as appropriate.
0
mokeefe
12/13/2006 5:55:09 AM

So you mean the DataValue I am pullling from the mls_id? I did notice something, it seems to be stubborn because of the same data values as you described:

 

<asp:SqlDataSource ID="sqlCityByMLS" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="<%$ AppSettings:connectionstring %>" SelectCommandType="Text" SelectCommand="SELECT city, mls_id FROM pwaordev..tCityMaster WHERE mls_id <> '' "></asp:SqlDataSource>

id  city                 mls_id 
1 29 Palms            Y
 2 Acton                 F
 3 Adelanto              T
 4 Agoura                F
 5 Agoura Hills          F
 6 Agua Dulce            F
 7 Aguanga               M
 11 Alberhill                 M
 12 Alhambra              M
 13 Aliso Viejo           S
 14 Alpine                   G
 15 Alta Loma             M
 16 Altadena              L
 17 Anaheim               S

here are a few rows of the data. If I set the value to the city, the dropdown list posts back correctly with the selected city. There has to be a better way around this?

 

0
mebinici
12/13/2006 3:52:51 PM
I notice the when I choose Agoura, Agoura Hills, or Agua Dulce on the postback the dd sets to Acton?
0
mebinici
12/13/2006 4:09:36 PM

Your mls_id field is not unique, so you can't assign it to DataValueField.  Use the ID field instead.

List controls require a unique field for item values, since that is what they use to identify each item.

Cheers,
 


Peter Brunone
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.
0
PeterBrunone
12/13/2006 8:27:52 PM
PeterBrunone:

Your mls_id field is not unique, so you can't assign it to DataValueField.  Use the ID field instead.

List controls require a unique field for item values, since that is what they use to identify each item.

Cheers,
 

 Thanks!

 I understand now that the DataValueField needs to be unique. Hum, so how would I go about using the ID Field on a DropDownList? I am confused?

0
mebinici
12/13/2006 8:53:55 PM

Quite simply, like this: 

<asp:SqlDataSource ID="sqlCityByMLS" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="<%$ AppSettings:connectionstring %>" SelectCommandType="Text" SelectCommand="SELECT city, id FROM pwaordev..tCityMaster WHERE mls_id <> '' "></asp:SqlDataSource>

 <asp:dropDownList ID="outside_assoc_cities" runat="server" AutoPostBack="True" Enabled="False" DataTextField="city" DataValueField="id" Visible="False"></asp:dropDownList>
 

Cheers,
 


Peter Brunone
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.
0
PeterBrunone
12/13/2006 9:09:07 PM
PeterBrunone:

Quite simply, like this: 

<asp:SqlDataSource ID="sqlCityByMLS" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="<%$ AppSettings:connectionstring %>" SelectCommandType="Text" SelectCommand="SELECT city, id FROM pwaordev..tCityMaster WHERE mls_id <> '' "></asp:SqlDataSource>

 <asp:dropDownList ID="outside_assoc_cities" runat="server" AutoPostBack="True" Enabled="False" DataTextField="city" DataValueField="id" Visible="False"></asp:dropDownList>
 

Cheers,
 

 

Ah, pull the id as the datavalue, hum, that still wouldn't work for what I am trying to do. By me pulling the value of the mls_id I would then be able to hide/show another control depending on the value of the mls_id and case out a statement depending on the string. How else would this work? Would I need another table with the mls_id as the primary key, then the description of it and then break it down further? Any ideas would be greatful!

0
mebinici
12/13/2006 9:14:27 PM

    If you're doing this on postback, then you'd probably just grab the selected value(s) from the cities list, do a quick SELECT DISTINCT query to get all MLS IDs associated with those cities, and then use the resulting data to determine what you hide or show down the line.

 


Peter Brunone
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.
0
PeterBrunone
12/13/2006 9:26:48 PM

I put the test page up just to have a quick idea of how it is working.

http://index3.pwaor.com/_test/Default.aspx?display_page=recip_form_request&agent_id=&member_id=&broker_id=&sesIsBroker=

I hope this explains what I am trying to do.

Thanks!

 

0
mebinici
12/13/2006 11:32:53 PM

As per all prior posts you must have a unique value on your list ite; HTML Select operation by design, not ASP.net Control.

If you require the mls_id to determine other display logic then you must look it up on postback, using the item ID (DropDownList.SelectedValue), and then action accordingly.

Eg.

Imports System.Data.SqlClient

 

Partial Class SQLDataSourceExample

    Inherits System.Web.UI.Page

 

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

 

        If Me.DropDownList1.SelectedIndex > -1 Then

 

            Dim ItemId As Integer = Integer.Parse(Me.DropDownList1.SelectedValue)

            Dim MLS_ID As String = Nothing

 

            Dim cmd As New SqlCommand

            Dim con As New SqlConnection

            cmd.Connection = con

            ' using connectionstring from SqlDataSource Control

            con.ConnectionString = Me.SqlDataSource1.ConnectionString

 

            cmd.CommandType = Data.CommandType.Text

            cmd.CommandText = "SELECT mls_id FROM pwaordev..tCityMaster WHERE ID=@ID"

 

            cmd.Parameters.Add("@ID", Data.SqlDbType.Int, 4).Value = ItemId

 

            Try

                con.Open()

                MLS_ID = cmd.ExecuteScalar()

                con.Dispose()

                cmd.Dispose()

            Catch ex As Exception

                con.Dispose()

                cmd.Dispose()

                Response.Write(ex.ToString())

            End Try

 

            ' Further display logic

            If Not String.IsNullOrEmpty(MLS_ID) Then

 

            End If

        End If

    End Sub

 

End Class

 

Rgds,

Martin.


Rgds,
Martin.

For the benefit of all users please mark any post answers as appropriate.
0
mokeefe
12/14/2006 2:07:13 AM
mokeefe:

As per all prior posts you must have a unique value on your list ite; HTML Select operation by design, not ASP.net Control.

If you require the mls_id to determine other display logic then you must look it up on postback, using the item ID (DropDownList.SelectedValue), and then action accordingly.

Eg.

Imports System.Data.SqlClient

 

Partial Class SQLDataSourceExample

    Inherits System.Web.UI.Page

 

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

 

        If Me.DropDownList1.SelectedIndex > -1 Then

 

            Dim ItemId As Integer = Integer.Parse(Me.DropDownList1.SelectedValue)

            Dim MLS_ID As String = Nothing

 

            Dim cmd As New SqlCommand

            Dim con As New SqlConnection

            cmd.Connection = con

            ' using connectionstring from SqlDataSource Control

            con.ConnectionString = Me.SqlDataSource1.ConnectionString

 

            cmd.CommandType = Data.CommandType.Text

            cmd.CommandText = "SELECT mls_id FROM pwaordev..tCityMaster WHERE ID=@ID"

 

            cmd.Parameters.Add("@ID", Data.SqlDbType.Int, 4).Value = ItemId

 

            Try

                con.Open()

                MLS_ID = cmd.ExecuteScalar()

                con.Dispose()

                cmd.Dispose()

            Catch ex As Exception

                con.Dispose()

                cmd.Dispose()

                Response.Write(ex.ToString())

            End Try

 

            ' Further display logic

            If Not String.IsNullOrEmpty(MLS_ID) Then

 

            End If

        End If

    End Sub

 

End Class

 

Rgds,

Martin.

 Wow, that is an advanced method! I now see how this works, from a programatic stand point it makes sense and seeing how powerful this is, I am in awe! Worked like a charm, now I can manipulate the other controls.

 

Thank you!

0
mebinici
12/14/2006 6:37:39 PM
Reply:

Similar Artilces:

Load Text box of one web form with selected list item of list box of another web form
one help please Problem is i have two web forms  1.  ForiegnkeyTest.aspx  2. foroginkeytable.aspx   step 1 1 ForiegnkeyTest.aspx one web form <head runat="server"><script language="javascript" type="text/javascript">   function popWin()   {         var confirmWin = null;      confirmWin = window.open('foroginkeytable.aspx','anycontent',   'width=455,height=435,status');   }  </script> &...

Cannot Get Text Value Drop Down List In Form View Get NULL exception instead
Ok, I have a page with AJAX Tab Control, within a tab, I have a formview with checkboxes and a dropdownlist, I am trying to use the findcontrol to get the text value retuned when I select a value.  In this case an ageFrom and a AgeTo.  The findcontrol works fine when using it with checkbox controls, but when I try to use it with the dropdownlist control, I have been getting the NULL Exception error.  I have spent the greater part of the morning looking for an answer and yet to find what I am looking for.  I am using VB so please respond back with information refering to V...

Drop down menu text mixed in with background text on form
I’ve added a menu to my web page (grey background white text). When I run the page and use the menu the drop down part I can see text (yellow) from the main web page as if it is in front of the drop down menu. I’ve even used the ‘bring to front’ option on the menu and ‘send to back’ on the web forms text.   Please, what have I done wrong   Are you doing something with absolute positioning,?  It sounds like your drop down list has a lower zIndex than whatever is blocking it. Can you post a small sample; it's tough to tell whats going on by your post. -DamienViso...

Text as text
Name: JohnA Boice Email: jaboiceathughesdotnet Product: Firefox Summary: Text as text Comments: You need to do as Safari does and make ALL text, including buttons, links, bookmarks, and general page text actual text so screen readers can interpret and voice the text. Until this occurs, your products are a real pain to use. And I have limited sight. What about those with less or no sight? Browser Details: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3 ...

Drop Down List (Other than current Text displayed) Populating Multiple Text Boxes
In my form’s EditItemTemplate I have a Dropdownlist that displays a list of People’s Names.   When I select a particular “Name” from the list, I want to populate multiple text boxes with, for example: One TextBox for Address/Street Name, One for City, State and Zip etc…(All fields are part of the record for the person named). Any and all suggestions on how to accomplish this, or already existing examples would be appreciated. Thanks   I assume you're meaning that you're using a FormView control. If so, within the DropDownList.SelectedIndexChange...

setting textfield text to value from drop down list
I have a drop down list which is populated from a query to the database in the Page_Load method. I also have a textfield and I want the value of this to be set to the selected value of the drop down list whenever the user selects a new value. I have the code: tf.setText(ddl.selectedItem.value.ToString()); However, when I select something it always seems to put the value of the first element of the drop down list into the textfield instead of the value selected. Can anybody help me out here? Thanks, Wallace In your Page_Load method, are you ONLY binding the d...

Adding a text, value pair to a drop down list
Hello, This should be simple (sorry I am posting it here, but can't find any info anywhere). I have an empty drop down list and want to add a text/value combination to it. Just need to know the syntax on adding these values to the drop down, and then also how to retrieve the selected item and value. I am not reading from a db and binding to the control. I want to be able to add the text and value items to the control through lines of code. Whatever syntax I've tried to do this is wrong! Any help on this would be appreciated. Thanks! Tom ddl.Items.Insert(0,new ListItem...

Drop Down List Value Verse Text Displayed
I have a DropDownList (ddLevel) that is bound to the LevelID field of a table, but displays the LevelText field for the user.  Works fine. I am trying to access the text displayed, not the actual value in VB.NET code behind.  I have tried the following to pieces of code. ddLevel.DataTextField and ddLevel.Text What am I doing wrong?  Thanks for any help or assistance.   You should setddlevel.DataTextField = "LevelText" ddlevel.DataValueField = "LevelID " And when You want to fatch value than You can Use ddlevel.s...

Assigning value and text to drop down list items.
Hello, I'm trying to figure out how to assigning value and text to drop down list items. I can easily assign the text value, but how can I assign the value item. I use the code below to populate my drop down list. Dim SQL As String = "SELECT fldNAME, ID FROM tbl_cate" Dim MyConn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ConfigurationSettings.AppSettings("db_path")) Dim MyCMD As New OleDb.OleDbCommand(SQL, MyConn) Dim MyRead As OleDb.OleDbDataReader MyConn.Open() MyRead = MyCMD.ExecuteReader W...

Adding Text Field Values of a Drop Down List
Hi I am trying to populate Text field values of a DropDownList like following: DataSet cat =  Business.Category.GetCategoriesByType(1, relationType, "");    CategoryList.DataSource= cat.Tables[0];CategoryList.DataTextField =cat.Tables[0].Columns[1].ToString() + " ( "+ cat.Tables[0].Columns[2].ToString() + " )";   CategoryList.DataValueField=cat.Tables[0].Columns[0].ToString();     CategoryList.DataBind(); However, I am getting run time error for CategoryList.DataTextField =cat.Tables[0].....: "DataBinder.Eval: 'System.Data.Data...

Dynamically producing a Drop Down List with both Text and Values
Hi,I'm trying to create a drop down list which has different values for different users (soom users will have 4 options in their list others will have six). So I'm trying to create the list dynamically. I check what type of user they are, then in my Page_Load method a call my generateList() method which takes in the user type and then creates the values for the drop down list.To create the list I'm using the example I found in the quickstart docs, which uses an ArrayList, like this:ArrayList ddlValues = new ArrayList();          ddlValues.Add("Acc...

Drop down list in Form view returns error when value in bound field (data binding
I get the following error when I click edit to edit the details in the web form:'ddlUserRestrictionType' has a SelectedValue which is invalid because it does not exist in the list of items.Parameter name: value  I would like it to accept any value which currently exists in the database. Is this easily achievable? With what you've posted, all I can tell is that you are trying to set the SelectedValue of a ddl to a value that does not exist in the list.  It would be helpful if you can post some code and the expected values of this control.~ Timwww.bucketo...

Check if the text box "text" has changed (RePost from web forms forum)
Greetings! How can i know if the "text" inside of a text box has changed? For example, I currently have this: if (txtMU.TextChanged()) { } But this does not work. The other idea I had was to assign the existing value of the text box to a string and then when the user click the save button I can create a conditional statement to compare the original string with the new string. However, I would like something more effective as I have like 12 text boxes to compare. Thanks, EduardoThanks,Please Mark this post as answered if it was helpful. Here is the easiest way that I have ...

Populate Text Boxes using the value from a Drop Down List
I have a drop down box that contains values retreived from an Access database. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load DDLSelectCust.DataSource = GetCompanyInfo DDLSelectCust.DataMember = "Customers" DDLSelectCust.DataTextField = "CompanyName" DDLSelectCust.DataValueField = "CustomerID" DDLSelectCust.DataBind() End Sub I have several other text boxes on the same page. How can I use the ...

Web resources about - Drop down list text value not posting back with correct text? - asp.net.web-forms

Posting style - Wikipedia, the free encyclopedia
... is replied to in e-mail , Internet forums , or Usenet , the original can often be included, or "quoted", in a variety of different posting styles ...

Running Cross-Country, Posting On Facebook, And Writing A Book About It All
Running a marathon is tough, but what about running a marathon each day, for 93 days in a row, from California to Florida? Registered nurse Helene ...

Page updates not posting to my twitter... - Facebook-Hilfebereich - Facebook
Facebook is a social utility that connects people with friends and others who work, study and live around them. People use Facebook to keep ...

Posting to the Stream in Real Time with the Live Stream Box
Posting to the Stream in Real Time with the Live Stream Box

Facebook Careers Postings: Patents, Engineering, Mobile and More
... deal with patents, mobile, sales and recruiting. We gather this information from the company’s Careers Page, and this week there were no postings ...

RI Doctor Fired For Posting About Patient On Facebook
The Rhode Island Board of Medical Licensure and Discipline has disciplined a physician for posting patient information on Facebook.

New grade book and posting options are now part of iTunes U
... with 4K video support for the iPad Air 2 or You can now search all of Facebooks 2 trillion public posts . iTunes U New grade book and posting ...

San Jose teen cited for child porn after posting classmates’ nudes on Instagram
The San Jose Mercury News in California reported that a 17-year-old student has been criminally cited for distributing child porn through an ...

Job postings reveal Amazon plans to take worldwide delivery, transportation into its own hands
Amazon.com (Nasdaq: AMZN) is in the midst of putting together a team to take delivery matters into its own hands, and the plan isn't limited ...

Craigslist is now full of men posting 'Netflix and chill' ads
... and chill" is being invoked. Check out this ad for a TV — a.k.a. "NETFLIX AND CHILL MACHINE!!" There's only one question left: does posting ...

Resources last updated: 11/23/2015 8:16:09 AM