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

Discussion in 'C#' started by binici, Dec 12, 2006.

  1. #1
    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.
     
    binici, Dec 12, 2006 IP
  2. Ravikumar

    Ravikumar Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi binici,

    some times autopostback will give problem, it will not allow to select values some times, please make the autopostback to "false" then check it again i think problem will solve.
     
    Ravikumar, Dec 23, 2006 IP
  3. kajakske

    kajakske Well-Known Member

    Messages:
    139
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    165
    #3
    You might want to do a view source of your generated page to see what's actually in the dropdown ... Might be your values aren't unique or something ...
     
    kajakske, Dec 26, 2006 IP
  4. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #4
    Generally, when working with controls, make sure to do:

    If IsPostBack then
    ' Get the Data
    ' FILL the control (Bind Data)
    End If

    This will allow you to keep your viewstate and only fill the control once :)
     
    ccoonen, Dec 30, 2006 IP
  5. kajakske

    kajakske Well-Known Member

    Messages:
    139
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    165
    #5
    Actually, ...

    It's "Not IsPostBack" when you need to fill up the stuff ;)
     
    kajakske, Dec 30, 2006 IP
  6. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #6
    Shoot, yup, that's what I meant, hehe (sorry, been boozin for a while now ;)
     
    ccoonen, Dec 30, 2006 IP