LinqDataSource“选择时”事件不会在回发时触发

发布于 2024-09-24 04:46:38 字数 1538 浏览 3 评论 0原文

我有一个带有两个下拉列表控件的网页,每个控件都绑定到单独的 LinqDataSource 对象。一个显示类别列表,另一个显示文章列表。类别选择驱动文章列表(至少,这是我的预期行为)。文章列表也会根据用户的语言首选项进行过滤,存储在会话中并应用在 ArticleLinqDataSource_Selecting 事件处理程序中。

<asp:Label runat="server" Text="Category Code:" AssociatedControlID="CategoryDropDownList" />
<asp:DropDownList runat="server" ID="CategoryDropDownList" DataSourceID="CategoryLinqDataSource" DataValueField="CategoryID" DataTextField="CategoryCode" AutoPostBack="true" />
...
<asp:Label runat="server" Text="Article Code:" AssociatedControlID="ArticleCodeDropDown" />
<asp:DropDownList runat="server" ID="ArticleCodeDropDown" DataSourceID="ArticleLinqDataSource" DataValueField="ArticleID" DataTextField="ArticleCode" OnDataBound="ArticleCodeDropDown_DataBound" />
 ...
<asp:LinqDataSource runat="server" ID="CategoryLinqDataSource" 
  ContextTypeName="Article.Data.ArticleDataContext"
  TableName="Categories" Select="new (CategoryID, CategoryCode)">
</asp:LinqDataSource>
...
<asp:LinqDataSource runat="server" ID="ArticleLinqDataSource" 
  ContextTypeName="Arcicle.Data.ArticleDataContext"
  TableName="Articles" OrderBy="ArticleCode"
  Select="new (ArticleID, ArticleCode)"
  OnSelecting="ArticleLinqDataSource_Selecting">
</asp:LinqDataSource>

当页面首次加载时,这一切都工作正常。类别列表包含所有可用的类别值,并且选择列表中的第一个类别。第一个类别的相应文章会适当地显示在文章下拉控件中。但是,当我更改类别时(会发生回发,因为我将其设置为 AutoPostBack="true"),文章下拉列表不会刷新。换句话说,OnSelecting 事件不会刷新这是预期的行为吗?如果是,我该如何解决这个问题?

I have a web page with two dropdownlist controls, each bound to separate LinqDataSource objects. One displays a list of Categories and the other displays a list of Articles. The Category choice drives Article list (at least, that's my intended behaviour). The Article list is also filtered based on the users language preference, stored in the Session and applied in the ArticleLinqDataSource_Selecting event handler.

<asp:Label runat="server" Text="Category Code:" AssociatedControlID="CategoryDropDownList" />
<asp:DropDownList runat="server" ID="CategoryDropDownList" DataSourceID="CategoryLinqDataSource" DataValueField="CategoryID" DataTextField="CategoryCode" AutoPostBack="true" />
...
<asp:Label runat="server" Text="Article Code:" AssociatedControlID="ArticleCodeDropDown" />
<asp:DropDownList runat="server" ID="ArticleCodeDropDown" DataSourceID="ArticleLinqDataSource" DataValueField="ArticleID" DataTextField="ArticleCode" OnDataBound="ArticleCodeDropDown_DataBound" />
 ...
<asp:LinqDataSource runat="server" ID="CategoryLinqDataSource" 
  ContextTypeName="Article.Data.ArticleDataContext"
  TableName="Categories" Select="new (CategoryID, CategoryCode)">
</asp:LinqDataSource>
...
<asp:LinqDataSource runat="server" ID="ArticleLinqDataSource" 
  ContextTypeName="Arcicle.Data.ArticleDataContext"
  TableName="Articles" OrderBy="ArticleCode"
  Select="new (ArticleID, ArticleCode)"
  OnSelecting="ArticleLinqDataSource_Selecting">
</asp:LinqDataSource>

This all works fine when the page first loads. The Category list contains the all the available category values, and the first category in the list is selected. And the corresponding Articles for the first category are displayed appropriately in the Article dropdown control. However, when I change the category (a post-back happens because I have it set to AutoPostBack="true", the Article dropdown does not get refreshed. In other words, the OnSelecting event is not getting fired on subsequent postbacks. Is this the expected bahaviour? If it is, how do I get around this?

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

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

发布评论

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

评论(1

勿忘初心 2024-10-01 04:46:38

您需要在 CategoryDropDownList 的 SelectedIndexChanged 事件中对 DropDownList 调用 DataBind。它不会自行发生。

例如

Protected Sub CategoryDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CategoryDropDownList.SelectedIndexChanged
        ArticleCodeDropDown.DataBind()
End Sub

You need to call DataBind on the DropDownList in the CategoryDropDownList's SelectedIndexChanged event. It doesn't happen on its own.

e.g.

Protected Sub CategoryDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CategoryDropDownList.SelectedIndexChanged
        ArticleCodeDropDown.DataBind()
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文