简体   繁体   English

回帖后如何维护div内容?

[英]How to maintain div contents after post back?

I have a panel on a page which it's contents are changed client side. 我在页面上有一个面板,它的内容在客户端被更改。 For example, I have the following web form: 例如,我有以下Web表单:

<asp:panel id="panContents" runat="server">
    <span>Initial content</span>
</asp:panel>

The following jQuery snippet replaces the contents of the div client side when the page is first loaded: 首次加载页面时,以下jQuery代码段会替换div客户端的内容:

$("<%= panContents.ClientId =>")
    .html("<span>Some content to be maintained after postback</span>")

After the page is submitted and returned to the client, the contents of the panel reverts back to as when the page was first loaded: 页面提交并返回到客户端后,面板的内容将恢复为首次加载页面时的内容:

<div>
    <span>Initial content</span>
</div>

Is it possible to maintain the contents of the panel after post back so that it appears the same before being submitted? 回复后是否可以维护面板内容,以便在提交之前显示相同内容? So I end up with the following after post back: 回帖后我最终得到以下内容:

<div>
    <span>Some content to be maintained after postback</span>
</div>

This content exists only on a client, since Panel does not post any content to the server (somewhat obviously). 此内容仅存在于客户端,因为Panel不会向服务器发布任何内容(显然)。 The workaround you might want to apply to check if you need to run the snippet on the page load. 您可能希望应用的解决方法,以检查是否需要在页面加载上运行代码段。 For this you might want a hidden field with a flag. 为此,您可能需要带有标志的隐藏字段。

Here is a dirty proof of concept: 这是一个肮脏的概念证明:

<asp:HiddenField ID="ReplacePanelFlag" runat="server" Value="0" />

// check flag on page load
$(function() {
    if ($("<%= ReplacePanelFlag.ClientID %>").val() === "1") {
        //run snippet
    }
});

// in snippet make sure to set the flag to "1"
$("<%= panContents.ClientId =>")
.html("<span>Some content to be maintained after postback</span>");
$("<%= ReplacePanelFlag.ClientID %>").val("1");

The content of panel vanishes because you are just manipulating the DOM. 面板的内容消失了,因为你只是在操纵DOM。 If you want to retain the value of panel first put it into a hidden field and after post back reassign the text to panel 如果要保留面板的值,请先将其放入隐藏字段,然后将文本重新分配给面板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM