【发布时间】:2010-04-07 14:04:05
【问题描述】:
我正在构建一个 asp.net 自定义控件,其中有两个下拉列表:companyIdSelection 和 productFamilySelection。我在 Page_Load 处填充 companyIdSelection,并根据 companyIdSelection 中的选定项目填充 productFamilySelection。我正在使用 UpdatePanels为了实现这一点,但由于某种原因,每次我更新 companyIdSelection Page_Load 时都会被调用(据我所知,只有在重新加载整个页面时才会发生这种情况),再次重新加载列表并且用户选择的项目丢失了(所选项目始终是最前面的)。这是代码
<asp:UpdatePanel ID="updateFamilies"
runat="server"
UpdateMode="Always">
<ContentTemplate>
Company ID:<br>
<br></br>
<asp:DropDownList ID="companyIdSelection"
runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="companyIdSelection_SelectedIndexChanged">
</asp:DropDownList>
<br></br>
Product Family:
<br></br>
<asp:DropDownList ID="productFamilySelection" runat="server"
AutoPostBack="True"
onselectedindexchanged="productFamilySelection_SelectedIndexChanged">
</asp:DropDownList>
<br>
</ContentTemplate>
</asp:UpdatePanel>
protected void Page_Load(object sender, EventArgs e)
{
this.companyIdSelection.DataSource = companyIds(); //companyIds returns the object containing the initial data items
this.companyIdSelection.DataBind();
}
protected void companyIdSelection_SelectedIndexChanged(object sender, EventArgs e)
{
// Page_Load is called again for some reason before this method is called, so it
// resets the companyIdSelection
EngDbService s = new EngDbService();
productFamilySelection.DataSource = s.getProductFamilies(companyIdSelection.Text);
productFamilySelection.DataBind();
}
另外,我尝试将 UpdatePanel 的 UpdateMode 设置为“Conditional”并添加一个 asyncpostback 触发器 但结果是一样的。 我做错了什么?
PS: 我通过在 Page_Load 方法中使用 Page.IsPostBack 解决了更新问题,但我仍然希望尽可能避免完整的回发
【问题讨论】:
标签: c# asp.net updatepanel