Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Mar 2006
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

    Thanks!

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New Coder
    Join Date
    Mar 2006
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nikkiH View Post
    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?

  • #4
    New Coder
    Join Date
    Mar 2006
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the when I choose Agoura, Agoura Hills, or Agua Dulce on the postback the dd sets to Acton?

  • #5
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #6
    New Coder
    Join Date
    Mar 2006
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nikkiH View Post
    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!

  • #7
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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?

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #8
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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
    Code:
    <%@ 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
    Code:
    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

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #9
    New Coder
    Join Date
    Mar 2006
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nikkiH View Post
    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!

  • #10
    New Coder
    Join Date
    Mar 2006
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I put the test page up just to have a quick idea of how it is working.

    http://index3.pwaor.com/_test/Defaul...=&sesIsBroker=

    I hope this explains what I am trying to do.

    Thanks!

  • #11
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    "Firefox can't find the server at index3.pwaor.com"

    Do you still need help getting it to work like you want?

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #12
    New Coder
    Join Date
    Mar 2006
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nikkiH View Post
    "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!

  • #13
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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?
    Last edited by nikkiH; 12-14-2006 at 08:48 PM.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #14
    New Coder
    Join Date
    Mar 2006
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nikkiH View Post
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •