客户端JavaScript的ViewState更新在ASP .NET形式 [英] Client-side JavaScript ViewState Update in asp .net forms

查看:179
本文介绍了客户端JavaScript的ViewState更新在ASP .NET形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了这个问题,当我需要收回HTML控件我已经添加了客户端使用JavaScript的 回传后(由于服务器端验证 - 这是不可选)

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

请告诉我,如果我想要实现这一目标的方式是CR * p和有这样做的更好的方法。

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

基本上,我在做什么是克隆一个文本框控件多达10次的页面上,当用户点击添加储运进入从每次texboxes的价值在一个隐藏字段从$ C阅读后面的$ C。然而,这工作得很好,当服务器端验证未通过回传后,所有这些动态添加的(克隆的)texboxes消失,因为ViewState的什么都不知道关于他们。

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.

我正在考虑2种可能的解决方案,这两者似乎的哈克的:

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

  • 在隐藏字段使用存储的值重建的文档的onload所有克隆的文本框()

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

包裹在AJAX更新面板的形式,并把它外面的克隆texboxes,因此,没有刷新这部分回传画面

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

现在,是有可能以某种方式的更新的ViewState中,使之认识到我所使用的客户端脚本添加的HTML控件?任何更好的想法吗?我想和客户端脚本来实现这一点,因此不考虑对服务器端的克隆文本框,不好意思。

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.

推荐答案

您不能修改在客户端的ViewState中。如果你这样做,你会作废的视图状态和回发收到一个错误。

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

在你的情况,你可能要考虑使用JavaScript和jQuery来呈现文本框上的文档准备事件与存储在您的隐藏字段中的值。我建议采取看看jQuery的模板,特别是如果你可以存储在隐藏字段中的数据为JSON (http://weblogs.asp.net/scottgu/archive/2010/10/04/jquery-templates-data-link-and-globalization-accepted-as-official-jquery-plugins.aspx).

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

这篇关于客户端JavaScript的ViewState更新在ASP .NET形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆