简体   繁体   English

用ASP.NET AJAX填充列表后更新viewstate

[英]Update viewstate after populating a list with ASP.NET AJAX

I've got a dropdown list that is being populated via a webservice using ASP>NET AJAX. 我有一个下拉列表,该列表通过使用ASP> .NET AJAX的Web服务进行填充。 On the success callback of the method in javascript, I'm populating the dropdown via a loop: 在javascript中方法的成功回调中,我通过循环填充了下拉列表:

function populateDropDown(dropdownId, list, enable, showCount) {
    var dropdown = $get(dropdownId);
    dropdown.options.length = 1;    
    for (var i = 0; i < list.length; i++) {
        var opt = document.createElement("option");
        if (showCount) {
            opt.text = list[i].Name + ' (' + list[i].ChildCount + ')';
        } else {
            opt.text = list[i].Name;
        }
        opt.value = list[i].Name;
        dropdown.options.add(opt);
    }
    dropdown.disabled = !enable;    
}

However when I submit the form that this control is on, the control's list is always empty on postback. 但是,当我提交该控件处于打开状态的表单时,该控件的列表在回发时始终为空。 How do I get the populated lists data to persist over postback? 如何获取填充的列表数据以保留回发?

Edit: Maybe I'm coming at this backwards. 编辑:也许我正在倒退。 A better question would probably be, how do I populate a dropdown list from a webservice without having to use an updatepanel due to the full page lifecycle it has to run through? 一个更好的问题可能是,由于必须经历整个页面生命周期,如何在不使用updatepanel的情况下从Web服务填充下拉列表?

You need to use Request.Form for this - you can't encrypt ViewState on the fly from the client - it would defeat the whole point of it :). 为此,您需要使用Request.Form-您不能从客户端即时加密ViewState-它会破坏整个观点:)。

Edit: Responding to your Edit :) the Page Lifecycle is the thing that allows you to use the ViewState persistence in the first place. 编辑:响应您的编辑:)页面生命周期是允许您首先使用ViewState持久性的东西。 The control tree is handled there and, well, there's just no getting around it. 控制树在那里处理,好吧,它周围没有任何东西。

Request.Form is a perfectly viable way to do this - it will tell you the value of the selection. Request.Form是执行此操作的一种完美可行的方法-它会告诉您选择的值。 If you want to know all of the values, you could do some type of serialization to a hidden control. 如果您想知道所有值,则可以对隐藏的控件进行某种类型的序列化。

Ugly, yes, But that's why god (some call him ScottGu) invented ASP.NET MVC :). 丑陋的,是的,但这就是为什么上帝(有人称他为ScottGu)发明了ASP.NET MVC :)。

Although I'm not really sure how it does it the CascadingDropDown in the AJAX Control Toolkit does support this. 尽管我不太确定它是如何做到的,但AJAX控件工具包中的CascadingDropDown确实支持此功能。

This is the line that appears to do it: 这行似乎是这样做的:

AjaxControlToolkit.CascadingDropDownBehavior.callBaseMethod(this, 'set_ClientState', [ this._selectedValue+':::'+text ]);

But the simplest idea would be to put the selected value into a hidden input field for the postback event. 但是最简单的想法是将所选值放入回发事件的隐藏输入字段中。

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

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