开发者

How to retain AJAX data after a POST with limited AJAX requests

I'm using C# with .NET 4 (though as per company policy, we never use any ASP controls, only HTML inputs, so our pages aren't really all that much different from Rails or PHP pages).

My page is required to make a number of AJAX requests to populate a few <select>'s based on the values in other inputs on the form. The AJAX requests merely go to simple ASPX pages that return <option>'s that are retrieved from a database. When the user submits the form and it goes through some validation, and fails, I want to still retain the values in their inputs. What I am currently doing in my page_load function is storing all of the POST Request's form values in an object and then re-selecting them on the page. Problem is, I have to re-run all my AJAX requests when the page loads so that I can populate the <select>'s in order to re-select what the user had submitted at the post.

It's un开发者_如何学编程desirable to make so many AJAX requests as soon as the page reloads after a POST. Is there a better way I can save all the <option>'s so that I don't have to make all these AJAX calls after the page reloads?


How about saving the option values in a hidden input before posting so if the posted form doesnt pass server-side validation you will be able to have the option data in the Request.Form object and you will be able to re-populate the control on the server with these values and use some kind of if() condition to prevent AJAX functions being used on the returned page.


I was also in the same problem . The solution that worked for me was : To enable viewstate to true for page , if not for page , we can do the same for just list controls. Then even if some server-side error occurs ,list controls will retain their values.

Another solution to the same problem is :

Whenever ajax response comes , we can save it in a hidden input (as discussed above),but we don't need to have too many hidden inputs for the same , it can be handled by using just 1 hidden input variable . Whenever ajax response comes, we need to append the value of the same hidden input field (to differentiate we need to append the name of the listbox in that value) , then we can easily bind the controls without sending ajax calls again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜