简体   繁体   English

在UpdatePanel中回发后的淘汰赛绑定

[英]Knockout binding after postback in UpdatePanel

I have a page with a table, bound with Knockout inside an UpdatePanel. 我有一个带有表的页面,在UpdatePanel中与Knockout绑定。 My goal is to get the binding successfully applied after a postback. 我的目标是在回发后成功应用绑定。 Everything works as expected on initial page load, so the model is working. 一切都能在初始页面加载时按预期工作,因此该模型正在运行。

I'm using pageLoad() to call a function that gets the model's JSON data from a HiddenField that was set server side. 我正在使用pageLoad()调用一个从设置为服务器端的HiddenField获取模型的JSON数据的函数。 In the view I have each RadioButton's click data-bind set to a JS function outside the ViewModel that sets a hidden input field's value to the ID specified by the button, and then causes a server postback by clicking a hidden button client-side. 在视图中,我将每个RadioButton的click数据绑定设置为ViewModel之外的JS函数,该函数将隐藏的输入字段的值设置为按钮指定的ID,然后通过单击客户端的隐藏按钮来导致服务器回发。

Inside pageLoad() I'm using Sys.WebForms.PageRequestManager.getInstance().add_endRequest to specify that the binding helper function should be called after the server-side of the postback is complete. 在pageLoad()内部,我使用Sys.WebForms.PageRequestManager.getInstance()。add_endRequest来指定应在回发的服务器端完成后调用绑定帮助器函数。

My problems: 我的问题:

After being set server-side, the HiddenField with the JSON data still has the same string as it did client-side when it returns to the client, even though when the server-side scriptlet is executed, I can see that the value updated correctly. 设置服务器端后,带有JSON数据的HiddenField返回客户端时仍具有与客户端相同的字符串,即使执行服务器端scriptlet时,我也可以看到该值已正确更新。 Not sure what is going on here. 不知道这是怎么回事。

Even if the values don't update, BindingHelper() is called, although for some reason clicking on the RadioButtons no longer causes the event after one postback has already occurred. 即使值不更新,也将调用BindingHelper(),尽管由于某种原因,单击RadioButtons不再会在发生一次回发后导致事件。

Any insight into this would be greatly appreciated! 任何对此的见解将不胜感激!

Code: 码:

<script type="text/javascript">

     Sys.WebForms.PageRequestManager.getInstance().add_endRequest(BindingHelper)

     function pageLoad() {
        if (!isPostBack()){
            BindingHelper();
        }
    }

    function BindingHelper() {
        var data = <%= hfData.Value%>;
        var model = new DataViewModel();

        model.load(data);
        ko.applyBindings(model);
    }

     function Select(id) {
        document.getElementById('<%=hfSelectedID.ClientID%>').value = id;
        document.getElementById('<%=rbSelect.ClientID%>').click();
    }

    function isPostBack() {
        return document.getElementById('hfPostback').value == 'True';
    }
</script>

HTML: HTML:

<table id="tblData">
    <thead>
        <tr>
            <th></th>
            <!-- ko foreach: subViewModel -->
                <th><input type="radio" name="rbGroup" data-bind="value: id, checked: $root.selectedID, click: function() {Select(id);}" /><span data-bind="text: name"></span></th>
            <!-- /ko -->
         </tr>
    </thead>
</table>

<asp:HiddenField ID="hfData" runat="server" />

<asp:HiddenField ID="hfSelectedID" runat="server" />
<input id="hfPostback" type="hidden" value="<%=Page.IsPostBack.ToString()%>" />

<div style="display:none">
    <asp:RadioButton ID="rbSelect" runat ="server" AutoPostBack="true" />
</div>

Edit: I've since determined that this works if the binding is not inside an UpdatePanel, just by registering BindingHelper() as a startup script in Page_Load. 编辑:我已经确定,如果绑定不在UpdatePanel内,则可以通过仅将BindingHelper()注册为Page_Load中的启动脚本来确定是否可行。

Oddly enough, even if I register the startup script by specifying the UpdatePanel, the results are the same. 奇怪的是,即使我通过指定UpdatePanel注册启动脚本,结果也是一样的。 There are other controls in the UpdatePanel so I know the UpdatePanel is in fact, updating, and it isn't set to conditionally update. UpdatePanel中还有其他控件,因此我知道UpdatePanel实际上正在更新,并且未设置为有条件地更新。

I solved this. 我解决了 I ended up needing to find the HiddenField in the DOM and then parsing the JSON from that to get the data for the model. 我最终需要在DOM中找到HiddenField,然后从中解析JSON以获取模型的数据。 It seems weird to me that even after the value had been set server-side, it was keeping the old value, but the UpdatePanel complicates the lifecycle. 在我看来,即使在服务器端设置了该值之后,它仍然保留了旧值,但UpdatePanel使生命周期变得复杂了。

Also I changed the RadioButton that is clicked client-side to a LinkButton to get it to always force a postback to the server. 另外,我将在客户端单击的RadioButton更改为LinkBut​​ton,以使其始终强制回发到服务器。

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

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