.net - Update viewstate after populating a list with ASP.NET AJAX - Stack Overflow

I've got a dropdown list that is being populated via a webservice using ASP>NET AJAX. On the su

I've got a dropdown list that is being populated via a webservice using ASP>NET AJAX. On the success callback of the method in javascript, I'm populating the dropdown via a loop:

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 ing 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?

I've got a dropdown list that is being populated via a webservice using ASP>NET AJAX. On the success callback of the method in javascript, I'm populating the dropdown via a loop:

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 ing 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?

Share Improve this question edited Sep 20, 2017 at 22:34 P.S. 16.4k14 gold badges65 silver badges86 bronze badges asked Oct 1, 2008 at 2:51 Glenn SlavenGlenn Slaven 34.2k27 gold badges117 silver badges166 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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 :).

Edit: Responding to your Edit :) the Page Lifecycle is the thing that allows you to use the ViewState persistence in the first place. 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. 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 :).

Although I'm not really sure how it does it the CascadingDropDown in the AJAX Control Toolkit does support this.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744757222a4591954.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信