看起來像這個問題was addressed here,但他的解決方案並不適用於我。我創建了一個動態下拉菜單系統,該系統使用基於第一個下拉列表中所選項目的查詢結果填充輔助下拉列表。DropDownList SelectedIndex值在AutoPostback上未更新
第一個下拉列表填充得到:得到填充
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)
第二個下拉:
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
第一個下拉的標記......
<asp:DropDownList ID="NewFaqDropDownCategory" AutoPostBack="true" runat="server" OnSelectedIndexChanged="NewFaqDropDownCategory_SelectedIndexChanged">
</asp:DropDownList>
而第二...
<asp:DropDownList ID="NewFaqDropDownList" runat="server" Visible="false">
</asp:DropDownList>
無論我嘗試了什麼,我總是會得到「1」(第二個下拉列表中第一項的值)。我之前引用的文章說這與AutoPostBack有關,服務器不知道該列表已更新。
任何人都可以澄清這一點對我來說多一點?
啊哈!你是完全正確的。我把休息放在那裏,發現事情在事件發生之前確實被調用了。我將DataBind封裝在if.page.ispostback條件中,並且解決了它!謝謝! – Anders 2009-03-03 20:31:36