【问题标题】:Updating a control outside the UpdatePanel在 UpdatePanel 之外更新控件
【发布时间】:2010-10-04 22:58:04
【问题描述】:

所以我有一个UserControl,上面有一些级联的DropDownLists。从列表 1 中选择会启用列表 2,而列表 2 会反过来启用列表 3。在所有三个列表中做出选择后,您可以转到下一页。

DropDownLists 都在UpdatePanel 中。但是“下一页”按钮位于UpdatePanel 之外。应该禁用该按钮,直到所有三个列表都有选择,然后应该再次启用它。但由于该按钮位于UpdatePanel 之外,因此在我进行选择时它不会更新。 (编辑:“下一页”按钮位于还包含UserControl 的页面上。)

我知道解决此问题的一种方法:

var scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(dropDownList1);
scriptManager.RegisterPostBackControl(dropDownList2);
scriptManager.RegisterPostBackControl(dropDownList3);

这可确保在更改任何下拉列表时进行回发,以便按钮可以更新。但如果我这样做,我可能会首先摆脱UpdatePanel 来简化。

是否有另一种方法,通过一些聪明的 JavaScript 或其他东西,我可以更新 UpdatePanel 之外的控件而不必放弃 Ajax?

【问题讨论】:

    标签: c# asp.net ajax asp.net-ajax updatepanel


    【解决方案1】:

    在下一个按钮周围放置一个 UpdatePanel,并为每个下拉菜单创建一个触发器,以便触发异步回发。例如:

    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="dropDownList1" />
        <asp:AsyncPostBackTrigger ControlID="dropDownList2" />
        <asp:AsyncPostBackTrigger ControlID="dropDownList3" />
    </Triggers>
    

    【讨论】:

      【解决方案2】:

      你不能在“下一页”周围添加一个更新面板,然后在下拉列表更新面板中添加一个触发器来触发下一页更新面板吗?

      只是提出想法,没有实际尝试过:)

      【讨论】:

      • 这是最接近我必须做的。而不是触发器,我只需要调用主页上的方法(强制转换 this.Page)来正确设置 Next 按钮。我还必须在 Next 按钮上注册 PostBackControl 才能让它在 UpdatePanel 中工作。
      【解决方案3】:

      您可以通过将控件放入 update_panel2 并说出 update_panel2.update() 来更新更新面板之外的控件。

      请注意,UpdatePanel 的 UpdateMode 必须设置为 conditional 才能正常工作。

      【讨论】:

        【解决方案4】:

        理想情况下,这将在 javascript 中完成,并在 click 事件处理程序中进行服务器端验证检查。

        一个快速的 jQuery 示例:

        //assuming the dropdowns all have the css class "cascade"
        $('select.cascade').change(function() {
          If ($('option:selected',this).length == 3) {
            $('input[id$=btnNext]').removeAttr('disabled');
          }
        });
        

        【讨论】:

          猜你喜欢
          • 2011-06-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多