DropDownList SelectedIndex value not updating on AutoPostback
Asked Answered
B

3

6

It looks like this question was addressed here, but his solution did not work for me. I am creating a dynamic dropdown menu system that populates a secondary dropdownlist with the results of a query based on the selected item in the first dropdown.

First dropdown getting populated:

Dim db As New linqclassesDataContext
Dim categories = (From c In db.faq_cats)

NewFaqDropDownCategory.DataSource = categories
NewFaqDropDownCategory.DataTextField = "category"
NewFaqDropDownCategory.DataValueField = "category_id"
NewFaqDropDownCategory.DataBind()
Unset(categories)
Unset(db)

Second dropdown getting populated:

Protected Sub NewFaqDropDownCategory_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim temp As Integer = CInt(Val(NewFaqDropDownCategory.SelectedIndex))
    MsgBox(theDrop.SelectedValue)
    Return

    'Dim db As New linqclassesDataContext
    'Dim faqs = (From f In db.faqs Where f.category = NewFaqDropDownCategory.SelectedValue)

    'NewFaqDropDownList.DataSource = faqs
    'NewFaqDropDownList.DataTextField = "question"
    'NewFaqDropDownList.DataValueField = "id"
    'NewFaqDropDownList.DataBind()
    'NewFaqLabel.Visible = True
    'NewFaqDropDownList.Visible = True
    'Unset(faqs)
    'Unset(db)
End Sub

The markup for the first dropdown...

<asp:DropDownList ID="NewFaqDropDownCategory" AutoPostBack="true" runat="server" OnSelectedIndexChanged="NewFaqDropDownCategory_SelectedIndexChanged">
</asp:DropDownList>

And the second...

<asp:DropDownList ID="NewFaqDropDownList" runat="server" Visible="false">
</asp:DropDownList>

No matter what I have tried, I always get "1" (the value of the first item in the second dropdown). The post I referenced earlier said this had to do with AutoPostBack and the server not knowing the list was updated yet.

Can anyone clarify this for me a little more?

Briannebriano answered 3/3, 2009 at 20:2 Comment(0)
S
9

Set a break point on the line that reads: NewFaqDropDownCategory.DataBind() and one in your event handler (NewFaqDropDownCategory_SelectedIndexChanged). I suspect the databind is being called right before your NewFaqDropDownCategory_SelectedIndexChanged event fires causing your selected value to change.

If so, you need either to make sure you only databind if you aren't in the middle of your autopostback or instead of using NewFaqDropDownCategory.SelectedIndex on the first line of your event handler you can cast the sender parameter to a DropDownList and use its selected value.

Scincoid answered 3/3, 2009 at 20:22 Comment(3)
ah hah! you were absolutely right. I put the break in there, found out it was indeed being called right before the event fired. I wrapped the DataBind in an if not page.ispostback conditional, and that fixed it up! thanks!Briannebriano
Yay, my SO virginity is lost with my first accepted answer :)Scincoid
Still helpful a decade later! I had nested dynamic controls with DataBind being called twice (once on the PlaceHolder filled with user controls, and once each for the PlaceHolder inside the user controls). I was baffled because the postback event would fire for everything EXCEPT the initial value, and even in the HTML rendering, the correct selected value was shown after I commented out the first, higher level databind, and wrapped the lower level one in a check for IsPostBack.Equilateral
I
2

I had the same problem. Found I forgot to look if I was posting back to the page or not and I was binding my DropDownList control in the Page_Load event of the page. I had forgot to use:

if (!IsPostBack)
{
   .... do databind ....
}
Ileana answered 20/4, 2009 at 11:49 Comment(0)
K
0

I think there is a bug in your LINQ query for the second drop down box

Dim faqs = (From f In db.faqs Where f.category = NewFaqDropDownCategory.SelectedValue)

Here you are comparing SelectedValue to category. Yet in the first combobox you said that the DataValueField should be category_id. Try changing f.category to f.category_id

Koan answered 3/3, 2009 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.