DropDownList SelectedIndex value not updated on AutoPostback

It seems this question has been addressed here , but its solution did not work for me. I create a dynamic drop-down menu that populates a secondary drop-down list with query results based on the selected item in the first drop-down list.

The first drop-down list is 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)

The second drop-down list is 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

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 tried, I always get "1" (the value of the first item in the second drop-down list). The message I referred to earlier said it was related to AutoPostBack, and the server, not knowing that the list was updated.

Can anyone clarify this for me a little more?

+5
3

, : NewFaqDropDownCategory.DataBind() (NewFaqDropDownCategory_SelectedIndexChanged). , databind , NewFaqDropDownCategory_SelectedIndexChanged .

, , databind, , NewFaqDropDownCategory.SelectedIndex DropDownList .

+8

. , , , DropDownList Page_Load . :

if (!IsPostBack)
{
   .... do databind ....
}
+2

I think there is an error in your LINQ query for the second popup

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

Here you compare SelectedValue with a category. However, in the first combo box, you said that DataValueField should be category_id. Try changing f.category to f.category_id

0
source

All Articles