最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Client-side JavaScript ViewState Update in asp .net forms - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Feb 3, 2011 at 15:47 RuslanRuslan 10.2k15 gold badges57 silver badges92 bronze badges 1
  • I went a pretty dirty way of adding 10 hidden controls on the page. This way ViewState is always aware of them and I can hide/unhide them to the client via JavaScript. – Ruslan Commented Mar 14, 2011 at 16:46
Add a ment  | 

1 Answer 1

Reset to default 5

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 remend taking a look at jQuery templating, particularly if you can store your data as JSON in the hidden field (http://weblogs.asp/scottgu/archive/2010/10/04/jquery-templates-data-link-and-globalization-accepted-as-official-jquery-plugins.aspx).

发布评论

评论列表(0)

  1. 暂无评论