Client-side JavaScript ViewState Update in asp .net forms

 ̄綄美尐妖づ 提交于 2019-12-01 05:43:45

问题


I have stumbled upon the issue when I need to retract html controls I've added client-side using JavaScript after the postback (due to server-side validation - this is not optional).

Please tell me if the way I'm trying to achieve this is cr*p and there's a better way of doing this.

basically, what I'm doing is cloning a textbox control for up to 10 times on the page when the user hits "Add" and storing entered values from each of those texboxes in a hidden field to read from in the code behind. This works fine, however, when the server side validation doesn't pass after postback, all those dynamically added (cloned) texboxes disappear, since ViewState knows nothing about them.

I am considering 2 possible solution, both of which seem hacky:

  • Rebuild all cloned textboxes on document onload() using stored values in the hidden field

  • wrap the form in ajax update panel and place the cloned texboxes outside of it, thus, not refreshing this part of the screen on postback

now, is it possible to somehow "update" ViewState to make it aware of all the html controls I've added using client-side script? Any better ideas? I'd like to achieve this with client-side script, therefore not considering cloning textboxes on server-side, sorry.


回答1:


You cannot modify the ViewState on the client side. If you do, you will invalidate the viewstate and receive an error on the postback.

In your case you might want to consider using javascript and jQuery to render the text boxes on the document ready event with the values stored in your hidden field. I'd recommend taking a look at jQuery templating, particularly if you can store your data as JSON in the hidden field (http://weblogs.asp.net/scottgu/archive/2010/10/04/jquery-templates-data-link-and-globalization-accepted-as-official-jquery-plugins.aspx).



来源:https://stackoverflow.com/questions/4888280/client-side-javascript-viewstate-update-in-asp-net-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!