views:

3470

answers:

4

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?

A: 

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
+4  A: 

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
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
Yay, my SO virginity is lost with my first accepted answer :)
grenade
A: 

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 .... }

A: 

I am doing the same thing (one dropdown populated by the selection in others), and the combination of what you have done worked for me:

In Page_Load, do an initial data binding of all drop downs (only for !IsPostBack).

When the parent drop downs change, re-data bind the child drop downs in the SelectedIndexChanged function, NOT in Page_Load.

I had been re-binding on Page_Load and the binding wasn't working correctly.

Thanks for the help!

Rahul Vaidya