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?

Community
  • 1
  • 1
Anders
  • 12,088
  • 34
  • 98
  • 146

3 Answers3

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.

grenade
  • 31,451
  • 23
  • 97
  • 126
  • 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! – Anders Mar 03 '09 at 20:31
  • 3
    Yay, my SO virginity is lost with my first accepted answer :) – grenade Mar 03 '09 at 20:39
  • 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. – Tim Dec 11 '20 at 17:34
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 ....
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
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

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454