ASP.NET UpdatePanel无法正常工作,它会刷新整个页面

时间:2015-04-10 10:50:16

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

我是使用UpdatePanel的新手,我有2个DropDownList: DropDownList_1DropDownList_2 其中DropDownList_2内容将取决于DropDownList_1所选值,这是我的代码:

ASPX:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:DropDownList ID="DropDownList_1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList_1_SelectedIndexChanged" DataSourceID="businessgroup" DataTextField="BusinessGroupName" DataValueField="Id"></asp:DropDownList>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="DropDownList_1" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>

<asp:DropDownList ID="DropDownList_2" runat="server" DataSourceID="jobposition" DataTextField="JobPositionName" DataValueField="Id"></asp:DropDownList>

CS:

protected void DropDownList_1_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlDataSource1.SelectCommand = "SELECT DISTINCT * FROM [JobPosition] WHERE BusinessGroupID ="+DropDownList_1.SelectedValue;
    DropDownList_2.DataBind();
}

它的工作如上所述,但我在输出中不想要的是整个页面刷新,我也尝试删除我的DropDownList_1中的AutoPostBack="true"但它停止工作,我在这里做错了什么?谢谢!

修改

我还尝试在UpdatePanel的ContentTemplate中移动DropDownList_2,但整个页面仍在刷新。

4 个答案:

答案 0 :(得分:1)

我找到了修复程序,感谢Cristina提醒我检查控制台错误。只是为了参考这里遇到同样问题的人,我所做的就是:

  1. 我缺少Ajax库,因此我在此文件夹Scripts&gt; WebForms&gt; MSAjax中导入了MicrosoftAjax.js和MicrosoftAjaxWebService。
  2. 导入必要的Ajax库后,我遇到了此控制台错误:
  3.   

    MicrosoftAjaxWebForms.js:6未捕获   Sys.WebForms.PageRequestManagerServerErrorException:   Sys.WebForms.PageRequestManagerServerErrorException:无效的回发   或回调参数。使用配置或&lt;%@ Page启用事件验证   EnableEventValidation =&#34;真&#34; %GT;在一个页面中。出于安全考虑,   此功能验证回发或回调事件的参数   源自最初呈现它们的服务器控件。如果   数据有效且预期,使用   ClientScriptManager.RegisterForEventValidation方法   注册回发或回调数据以进行验证。

    我所做的是在此Ajax页面中添加EnableEventValidation="false",在&lt;%Page%&gt;中指令。

    之后我不再需要整页重新加载,现在所有人都在按照我想要的方式工作。

答案 1 :(得分:0)

您应该如何做:

  • 如果要刷新第二个下拉列表,则第二个下拉列表应位于更新面板中

  • 仅为第二个下拉列表设置AutoPostBack="true"

  • 为更新面板设置UpdateMode="Conditional"(否则每次都会刷新)
  • 将面板的AsyncPostBackTrigger设置为指向第一个下拉SelectedIndexChanged事件
  • 为“更新”面板设置ChildrenAsTriggers="true"

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>     
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
                <ContentTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList_1_SelectedIndexChanged" DataSourceID="businessgroup" DataTextField="BusinessGroupName" DataValueField="Id"></asp:DropDownList>
                    <asp:DropDownList ID="DropDownList_2" runat="server" AutoPostBack="true" DataSourceID="jobposition" DataTextField="JobPositionName" DataValueField="Id"></asp:DropDownList>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="DropDownList_1" EventName="SelectedIndexChanged" />
                </Triggers>
            </asp:UpdatePanel>

控件应该位于同一个更新面板中,这种方式更简单。

答案 2 :(得分:0)

在使用更新面板时,您必须在刷新页面时注册您的事件,因为您必须使用Microsoft的PageRequestManager 重新订阅每次更新。

您必须在document.ready(function(){})初始化您的活动 javascript

例如:var test=Sys.WebForms.PageRequestManager.getInstance();

答案 3 :(得分:0)

如果按照指南进行设置后面板仍然回发,请检查是否未对执行回调的特定控件或通过将ClientIDMode默认设置为static继承自ClientIDMode="Static"在web.config,Page或父容器中。

在解决方案中搜索ClientIDMode="Static",然后为继承的控件(包括触发回发的控件)更改此解决方案,或者为触发回发的每个控件显式设置ClientIDMode="Predictable"ClientIDMode="AutoID"

相关问题