DropDownList SelectedIndex 值未在 AutoPostback 上更新

发布于 2024-07-14 15:08:37 字数 1735 浏览 7 评论 0原文

看起来这个问题已在此处解决,但是他的解决方案对我不起作用。 我正在创建一个动态下拉菜单系统,该系统使用基于第一个下拉列表中所选项目的查询结果填充辅助下拉列表。

第一个下拉列表已填充:

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 有关,并且服务器还不知道列表已更新。

谁能帮我进一步澄清一下吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愁杀 2024-07-21 15:08:37

在 NewFaqDropDownCategory.DataBind() 行和事件处理程序 (NewFaqDropDownCategory_SelectedIndexChanged) 中设置一个断点。
我怀疑在 NewFaqDropDownCategory_SelectedIndexChanged 事件触发之前调用数据绑定,导致您选择的值发生更改。

如果是这样,您需要确保仅在不在自动回发过程中时才进行数据绑定,或者您可以将 sender 参数强制转换为 DropDownList 并使用其事件处理程序的第一行,而不是使用 NewFaqDropDownCategory.SelectedIndex选定的值。

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.

寂寞清仓 2024-07-21 15:08:37

我有同样的问题。 发现我忘记查看是否回发到页面,并且我在页面的 Page_Load 事件中绑定了 DropDownList 控件。
我忘记使用:

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

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 ....
}
怪我鬧 2024-07-21 15:08:37

我认为您的第二个下拉框的 LINQ 查询中存在错误,

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

您正在将 SelectedValue 与类别进行比较。 然而,在第一个组合框中,您说 DataValueField 应该是category_id。 尝试将 f.category 更改为 f.category_id

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文