View Full Version : Drop down list text value not posting back with correct text?
binici
12-13-2006, 01:35 AM
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.
Thanks!
nikkiH
12-13-2006, 02:19 AM
In page_load, make sure you're checking if something is a postback before calling databind all over.
If not Page.IsPostBack Then
' do databinds and whatnot
End If
binici
12-13-2006, 04:58 PM
In page_load, make sure you're checking if something is a postback before calling databind all over.
If not Page.IsPostBack Then
' do databinds and whatnot
End If
Thank you for your reply, I did that as well:
If (Not Page.IsPostBack) Then
outside_assoc_cities.DataSource = sqlCityByMLS.Select(DataSourceSelectArguments.Empty)
outside_assoc_cities.DataBind()
outside_assoc_cities.Items.Insert(0, "(Select City)")
outside_assoc_cities.Items.Item(0).Value = "0"
outside_assoc_cities.SelectedIndex = 0
End If
I even tried straight from the sqldatasource, but I noticed something. Below is the sqldatasource code and data from my table.
<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. It only acts weird when I set the value of the dd to the mls_id and I am suspected because the data is not a unique value?
binici
12-13-2006, 05:32 PM
the when I choose Agoura, Agoura Hills, or Agua Dulce on the postback the dd sets to Acton?
nikkiH
12-13-2006, 08:38 PM
I'm relatively sure the id (value) needs to be unique or it would end up selecting whichever instance it happened to find first that matched the ID.
I can't think of how else it would know the difference between two listItems, unless you wrote some code to make it use the text instead of the value.
binici
12-13-2006, 09:49 PM
I'm relatively sure the id (value) needs to be unique or it would end up selecting whichever instance it happened to find first that matched the ID.
I can't think of how else it would know the difference between two listItems, unless you wrote some code to make it use the text instead of the value.
I figured that was the case, so I said screw it and this is what I did:
Set the value and text to the city, which it works, so now I added a sqldatasource to pass the city value into a stored procedure to get the mls_id by the city value.
Now, this is my problem:
<asp:Repeater ID="city_mls_desc" runat="server" DataSourceID="sqlGetMLSByCity" Visible="False">
<ItemTemplate>
The MLS system that covers <%#Eval("city")%> is <%#Eval("description") %>. <asp:Label ID="mls_id" runat="server" Text='<%#Eval("mls_id")%>'></asp:Label>
</ItemTemplate>
</asp:Repeater> <br />
I need to set this <asp:Label ID="test" runat="server"></asp:Label> to the value retrieved by <asp:Label ID="mls_id" runat="server" Text='<%#Eval("mls_id")%>'></asp:Label> ?
I believe I have to use this eventarg?
Protected Sub city_mls_desc_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles city_mls_desc.ItemCreated
Dim txtMLSId As Label
txtMLSId = CType(e.Item.FindControl("mld_id"), Label)
test.Text = txtMLSId.Text
End Sub
but its not working? I don't receive a value. The reason I need to retrieve the value is to check the mls_id which will trigger another control on and off depending on the mls_id value.
Any ideas?
Thanks!
nikkiH
12-13-2006, 10:32 PM
Back up a tad, I think you're overcomplicating things...
You just want to be able to get the mls_id that corresponds to the item the user chose in the DropDownList, right?
nikkiH
12-13-2006, 11:00 PM
This is a simple example of using a sqldatasource to fill a dropdownlist, then getting a different value from it onselectedindexchanged.
I used Northwind, so it's testable.
aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Test</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="lbl" runat="server" />
<div>
<asp:SqlDataSource runat="server" ID="sqlda"
SelectCommand="SELECT [CategoryID], [CategoryName], [Description], [Picture] FROM [Northwind].[dbo].[Categories]"
ConnectionString = "<%$ ConnectionStrings:NORTHWIND %>"
ProviderName="System.Data.SqlClient" />
<asp:DropDownList ID="ddl" runat="server"
AutoPostBack="True" DataTextField="CategoryName"
DataValueField="CategoryID"
AppendDataBoundItems="True" DataSourceID = "sqlda">
<asp:ListItem Text="(choose one)" Value="0" />
</asp:DropDownList>
</div>
</form>
</body>
</html>
code behind
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub ddl_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl.SelectedIndexChanged
lbl.Text = "selected: " & ddl.SelectedValue
Dim dv As System.Data.DataView
dv = CType(sqlda.Select(New DataSourceSelectArguments), DataView)
dv.RowFilter = "CategoryID=" & ddl.SelectedValue
lbl.Text = lbl.Text & "<br>" & dv(0)("Description")
End Sub
End Class
binici
12-13-2006, 11:48 PM
Back up a tad, I think you're overcomplicating things...
You just want to be able to get the mls_id that corresponds to the item the user chose in the DropDownList, right?
That is correct! Thanks, I will try that method. So basically, the dropdown will have the text and data of the city column and when that is choosen, I can then display the selected city and selected mls_id on a label control and then do other stuff that I would need to do.
Thanks!
binici
12-14-2006, 12:31 AM
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!
nikkiH
12-14-2006, 03:20 PM
"Firefox can't find the server at index3.pwaor.com"
Do you still need help getting it to work like you want?
binici
12-14-2006, 06:46 PM
"Firefox can't find the server at index3.pwaor.com"
Do you still need help getting it to work like you want?
Doh, Hum... wonder why its not coming up for FireFox?
I would truely appreciate if you could still help. I am so close, Let me know if you need to see anything else specific? Thank you!
nikkiH
12-14-2006, 08:46 PM
The DNS doesn't exist, not just firefox. I think you gave the wrong link.
Cannot find server or DNS Error
Internet Explorer
You said you were going to try the method -- did you try it?
What worked or didn't work?
You got the sqldatasource query getting ALL the fields instead of just the two for the dropdown, right?
binici
12-14-2006, 09:13 PM
The DNS doesn't exist, not just firefox. I think you gave the wrong link.
Cannot find server or DNS Error
Internet Explorer
You said you were going to try the method -- did you try it?
What worked or didn't work?
You got the sqldatasource query getting ALL the fields instead of just the two for the dropdown, right?
I used this event:
If Me.outside_assoc_cities.SelectedIndex > -1 Then
Dim ItemId As Integer = Integer.Parse(Me.outside_assoc_cities.SelectedValue)
Dim MLSID As String = Nothing
Dim cmd As New SqlCommand
Dim con As New SqlConnection
cmd.Connection = con
'using connectionstring from SqlDataSource Control
con.ConnectionString = Me.sqlGetMLSByCity.ConnectionString
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT mls_id FROM pwaordev..tCityMaster WHERE ID=@ID"
cmd.Parameters.Add("@ID", SqlDbType.Int, 4).Value = ItemId
Try
con.Open()
MLSID = cmd.ExecuteScalar
con.Dispose()
cmd.Dispose()
Catch ex As Exception
con.Dispose()
cmd.Dispose()
Response.Write(ex.ToString())
End Try
'Further logic to display button depending on MLS
If Not String.IsNullOrEmpty(MLSID) Then
If MLSID = "B" Then
city_mls_desc.Text = "The MLS system that covers " & outside_assoc_cities.SelectedItem.Text & " is Greater South Bay MLS."
page1_btn.Visible = True
Ect...
End if
End If
End Sub
This seems to work. Thanks for everything!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.