【问题标题】:How to prevent UpdatePanel from resetting values outside the panel?如何防止 UpdatePanel 重置面板外的值?
【发布时间】:2016-04-27 05:30:38
【问题描述】:

这是我的问题:

  1. 我使用 ddlType 选择类型
  2. 我点击 btnAddChoice 添加几个选项
  3. 我点击 btnSubmit 保存表单

但是当我尝试在 btnSubmit_Click() 中获取 ddlType.SelectedValue 时,它总是回到“选择类型”。

如果我不执行第 2 步,那么它可以正常工作。 据我了解,问题与 ajax 调用(回发)有关,可以通过将 SelectedValue 保存在 Session 或 ViewState 中来解决。

但是我在这个页面上有很多其他控件(例如 ddlType),所以它看起来不是一个优雅的解决方案。我希望框架能够自动将所选值保留在 ViewState 中……知道吗?毕竟,UpdatePanel 的目标不就是只更新面板吗?

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<asp:DropDownList ID="ddlType" runat="server">
    <asp:ListItem Text="Select a type" Value=""></asp:ListItem>
    <asp:ListItem Text="One" Value="1"></asp:ListItem>
    <asp:ListItem Text="Two" Value="2"></asp:ListItem>
    <asp:ListItem Text="Three" Value="3"></asp:ListItem>
</asp:DropDownList>

<div>
    <strong>Choices</strong>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnAddChoice" EventName="Click" />
        </Triggers>
        <ContentTemplate>
            <ul class="list-unstyled">
                <asp:PlaceHolder runat="server" ID="phChoices"></asp:PlaceHolder>
            </ul>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Button ID="btnAddChoice" runat="server" Text="Add Choice" CausesValidation="false" 
            OnClick="btnAddChoice_Click" />
</div>

<asp:Button ID="btnSubmit" runat="server" Text="Add question" 
                           OnClick="btnSubmit_Click" />

更新

部分问题是由以下原因引起的:

$('#<%= MainPanel.FindControl("btnAddChoice").ClientID %>').click(function () {
    var ddlType = $('#<%= MainPanel.FindControl("ddlType").ClientID %>');
    ddlType.attr('disabled', 'disabled');
});

详情请参阅question。 关于其他控件也是空的,原因是"dynamically created controls have to be created each and every post-back"

【问题讨论】:

  • 你的ScriptManager在哪里?
  • 好的,我已经将它添加到 sn-p 中(尽量保持简短)
  • 所以基本上你不想刷新整个页面?对吗?
  • 完全正确:当我单击 btnAddChoice 时,我只想更改 UpdatePanel 内容,它似乎工作正常...... ddlType 没有更新(视觉上),但在 ddlType 后面的代码中。 SelectedValue 被重置为默认值(空)
  • hmm 我在 sn-p 的顶部添加了 ScriptManager 标记 .. 有什么遗漏吗?

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


【解决方案1】:

尝试将这样的内容添加到您的ScriptManager

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />

那么你要刷新的所有代码都会在Update Panel

<asp:Updatepanel id="UpdatePanel1" runat="server">
        <ContentTemplate>
...All your HTML content here
</ContentTemplate>
    </asp:Updatepanel>

希望有帮助

更多关于EnablePartialRendering的信息

更新

此外,如果您的 dropdownlist 值由于 Updatepanel 而未保存

尝试Request.Form 并检查

Request.Form.Get("dropdownlistID").ToString());

【讨论】:

  • 谢谢...我已经尝试过了,但我遇到了同样的问题(顺便说一句,我认为 EnablePartialRendering 默认值为 true)
  • 您可以粘贴您尝试过的代码here 以便我也可以检查
  • 您选择的值没有得到正确保存?
  • 没错,ddlType.SelectedValue 显然是由 btnAddChoice 重置的,但在视觉上它很好
  • 好的,我刚刚发现了部分问题,我会更新我的问题,感谢您的帮助;)
猜你喜欢
  • 1970-01-01
  • 2016-07-04
  • 1970-01-01
  • 1970-01-01
  • 2022-12-13
  • 1970-01-01
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
相关资源
最近更新 更多