Div is hidden after submit in UpdatePanel

∥☆過路亽.° 提交于 2019-12-25 02:44:12

问题


I have a div inside an UpdatePanel, this div is shown when a use click on an edit link. The submit buttons to save are inside this div. Now when the use click on the submit button everything is fine except that this div is automatically hidden! the visibility is changed client side using jQuery's show().

Why is the UpdatePanel hiding my div even though it was shown by me? I have tried to set the runat='server' and enableing viewstate but I am getting the same result.

How do I just tell the UpdatePanelto leave thediv` as it is prior to the submit?

Here is a mini project that shows the problem:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                <a href="#" id="edit-link">edit</a>
            </div>
            <div id="edit-div" style="display:none; border: 2px black solid;">
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
</form>

<script>
    $(document).ready(function(){
        $('#edit-link').on('click', function () {
            $('#edit-div').show();
        });
    });
</script>

The code for the submit button:

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = DateTime.UtcNow.ToString();
}

回答1:


The simplest solution to this problem would be: -

  1. don't use jQuery to show edit-div
  2. make edit-div a server control
  3. convert your <a href="#" id="edit-link">edit</a> to an <asp:LinkButton> control, and in its server-side click event, show your edit div

The problem is that the UpdatePanel is restoring the original state as per the markup for the page; edit-div is not a server control and you are showing it via client script, the UpdatePanel is unaware of this and will always return you the original markup.

There are ways to get jQuery to play nice with UpdatePanels in these scenarios, but it is more involved than the simple solution above.

Hope that helps.



来源:https://stackoverflow.com/questions/20752745/div-is-hidden-after-submit-in-updatepanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!