【问题标题】:UpdatePanel reloads the whole pageUpdatePanel 重新加载整个页面
【发布时间】: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


    【解决方案1】:

    我认为您误解了 UpdatePanels 的工作原理。他们确实做了一个完整的页面回发,只是在渲染阶段他们只捕获一部分输出并将其发送回 AJAX 响应中,以便可以更新页面。 More info here.

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

    【讨论】:

      【解决方案2】:

      更新面板回调将在每次回调时通过页面加载。从表面上看,会发生拉页面生命周期(减去渲染和预渲染)。更新面板呈现 ajax 的外观,但您的客户端代码仍回发到同一页面 - 这是您所描述的问题。如果您可以避免使用更新面板,我建议您这样做。改用jQuery之类的东西。如果没有,请在您的 Page_Load 中使用它

      if (Page.IsCallback) { } //Do callback specific code here
      else { } //Do Postback specific code here
      

      希望这会有所帮助。祝你好运。

      【讨论】:

        猜你喜欢
        • 2011-06-24
        • 2011-03-24
        • 2013-09-18
        • 2016-04-19
        • 1970-01-01
        • 2012-11-23
        • 2013-10-28
        • 2016-10-26
        • 1970-01-01
        相关资源
        最近更新 更多