UpdatePanel重新加载整个页面

Emi*_*l D 1 c# asp.net updatepanel

我正在构建一个asp.net cutom控件,里面有两个下拉列表:companyIdSelection和productFamilySelection.I在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();
}
Run Code Online (Sandbox Code Playgroud)

此外,我尝试将UpdatePanel的UpdateMode设置为"Conditional"并添加asyncpostback触发器,但结果是相同的.我究竟做错了什么?

PS:我通过在Page_Load方法中使用Page.IsPostBack修复了更新问题,但我仍然希望尽可能避免完整的回发

pat*_*ech 12

我认为你误解了UpdatePanels的工作原理.他们实际上做了整页回发,只是在渲染阶段他们只捕获一部分输出并将其发送回AJAX响应,以便页面可以更新.更多信息在这里.

因此,您仍需要检查它是否是page_load事件中的回发,并且仅在不是一个时才执行数据加载.