ASP.NET UpdatePanel不工作,它会刷新整个页面



我是UpdatePanel的新手,我有两个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",但它停止了工作,我在这里做错了什么?谢谢

编辑:

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

以下是您应该如何做:

  • 如果你想刷新第二个下拉列表,那么第二个应该在更新面板内

  • 仅为第二个下拉设置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>

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

我找到了修复程序,感谢Cristina提醒我检查控制台错误。我所做的是:

  1. 我缺少Ajax库,所以我在Scripts>WebForms>MSAjax文件夹中导入了MicrosoftAjax.js和MicrosoftAjaxWebService
  2. 在导入必要的Ajax库后,我得到了以下控制台错误:

MicrosoftAjaxWebForms.js:6未捕获Sys.WebForms.PageRequestManager服务器错误异常:Sys.WebForms.PageRequestManagerServerErrorException:无效回发或回调参数。使用配置中或<%@启用事件验证页EnableEventValidation="true"%>。出于安全目的,该功能验证回发或回调事件的参数源于最初呈现它们的服务器控件。如果数据是有效的和预期的,请使用ClientScriptManager.RegisterForEventValidation方法,以便注册回发或回调数据以进行验证。

我所做的是在这个Ajax页面中添加EnableEventValidation="false",在<%Page%>指令。

在那之后,我再也没有完整的页面重新加载了,现在一切都在按照我的意愿进行。

如果您的面板在按照指导原则设置后仍然返回,请检查您是否没有为执行回调的特定控件设置ClientIDMode="Static",或者使ClientIDMode默认为从web.config、Page或父容器中继承的静态。

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

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

您必须在document.ready(function(){})中初始化您的事件CCD_ 18。

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

最新更新