开发者

non-server controls and partial postbacks

开发者 https://www.devze.com 2023-02-10 06:06 出处:网络
I\'m inserting some non-server controls (plain html controls) dynamically into an update panel using jQuer开发者_开发知识库y.

I'm inserting some non-server controls (plain html controls) dynamically into an update panel using jQuer开发者_开发知识库y.

If I do a full postback, I can get the values of those controls using Request.Form, however if I do a partial postback, I cannot.

Should I be able to get the values of html controls during a partial postback? Or only controls with runat="server"?

I am using non server controls as I'm playing around with facebox and file upload controls... it's not pretty :-|


update panel is not an efficient way of doing async.

As you're already using jQuery, add a web service (.asmx) to you project and hit that. See near figure 4 of this article: http://msdn.microsoft.com/en-us/magazine/cc163413.aspx

Request.Form is built from the viewstate, which your controls won't have been added to.


You can get all of the values of any form control or controls you want in a Partial Postback. It is a trivial answer, but not intuitively obvious at first glance. Maybe that's why ASP.net is dead.

Remember that the parameters of

__doPostback(string controlId,string param)

are strings, and the ASP.net methods

Request.Params.Get("__EVENTTARGET"); // id
Request.Params.Get("__EVENTTARGET"); // parameter

return strings.

Therefore the simple answer is to

  1. Construct a javascript object with key values pairs of all your the form elements that you wish to send back in your partial.

  2. Stringify that object, and pass it as the second parameter of your __doPostBack call.

On the server side you simply reverse this process to extract your object and all the form values you care about.

For example :

var strigifiedObject = JSON.stringify(myFormObject);
var id = "partialPostback";
__doPostBack(id, strigifiedObject);

... meanwhile on the server side

string id = Request.Params.Get("__EVENTTARGET");
string param = Request.Params.Get("__EVENTARGUMENT");

if (id == "partialPostback") {
    List<MyObj> formVals = new JavaScriptSerializer().Deserialize<List<MyObj>>>(param);
}
0

精彩评论

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

关注公众号