C# ASP.NET UpdatePanel不工作,将刷新整个页面

C# ASP.NET UpdatePanel不工作,将刷新整个页面,c#,asp.net,asp.net-ajax,updatepanel,autopostback,C#,Asp.net,Asp.net Ajax,Updatepanel,Autopostback,我刚开始使用UpdatePanel,我有两个下拉列表: DropDownList_1和DropDownList_2 其中DropDownList_2内容将取决于DropDownList_1所选值,以下是我的代码: ASPX: <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="serve

我刚开始使用
UpdatePanel
,我有两个下拉列表:
DropDownList_1
DropDownList_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>
protected void DropDownList_1_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlDataSource1.SelectCommand = "SELECT DISTINCT * FROM [JobPosition] WHERE BusinessGroupID ="+DropDownList_1.SelectedValue;
    DropDownList_2.DataBind();
}
如上所述,它可以正常工作,但我不想在输出中刷新整个页面,我还尝试删除DropDownList中的
AutoPostBack=“true”
,但它停止工作,我在这里做错了什么?谢谢

编辑:

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

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

以下是您应该做的:

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

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

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



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

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

  • 我缺少Ajax库,所以我将MicrosoftAjax.js和MicrosoftAjaxWebService导入到这个文件夹Scripts>WebForms>MSAjax中
  • 导入必要的Ajax库后,出现以下控制台错误:
  • MicrosoftAjaxWebForms.js:6未捕获 Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException:回发无效 或回调参数。在配置或页面中使用启用事件验证。为了安全起见, 此功能验证回发或回调事件的参数是否正确 源于最初呈现它们的服务器控件。如果 数据有效且符合要求,请使用 ClientScriptManager.RegisterForEventValidation方法,以便 注册回发或回调数据以进行验证

    我所做的是在这个Ajax页面中的指令中添加
    EnableEventValidation=“false”


    在此之后,我不再重新加载整个页面,现在一切都按照我的要求进行了。

    在使用“更新”面板时,您必须在刷新页面的同时注册事件,您必须使用Microsoft的PageRequestManager 重新订阅每个更新

    您必须在
    document.ready(function(){})
    javascript


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

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


    在您的解决方案中搜索
    clientdmode=“Static”
    ,并对继承的控件(包括触发回发的控件)进行更改,或者显式设置
    clientdmode=“predicatable”
    clientdmode=“AutoID”
    对于触发回发的每个控件。

    您的DropDownList_2在UpdatePanel之外,当您执行数据绑定()时,页面将被刷新。您好demonplus,谢谢您的评论,但我刚刚尝试将DropDownList_2移动到UpdatePanel的ContentTemplate中,但它不起作用。请确保AutoPostBack=“true”对于第一个DropDownList,这是必要的。如上所述,DropDownList_1具有AutoPostBack=“true”,但仍然会刷新整个页面。值得一提的是,即使这不适用于您手头的案例:
    UpdatePanel
    如果它的元素具有
    clientMode=“Static”
    ,则无法正常工作。嗨,克里斯蒂娜,谢谢,我很感谢你的回答,但是我的两个下拉列表的功能是全功能的,我这里唯一的问题是刷新整个页面而不是只刷新下拉列表2,我还尝试了你的一些建议,比如在更新面板中移动第二个下拉列表,添加UpdateMode=“Conditional”和ChildrenAsTriggers=“true”,在第二个下拉列表中移动autopostback,但输出仍然相同,整个页面仍会刷新。从第一个下拉列表中选择项目时是否会出现问题?这是回发的触发器吗?是的,没错,当我在DropDownList_1中选择一个项目时,整个页面会因为自动回发而刷新,这就是为什么我使用UpdatePanel,因为我不希望整个页面刷新,而只希望DropDownList_2刷新。我这边的其他建议是:1.检查javascript错误,2.检查更新面板是否不在表行中。此外,请尝试在脚本管理器上设置
    EnablePartialRendering=“true”