1

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?

4

1 回答 1

1

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

例如

Protected Sub CategoryDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CategoryDropDownList.SelectedIndexChanged
        ArticleCodeDropDown.DataBind()
End Sub
于 2010-10-21T00:29:41.953 回答