如何使用 jquery 制作高级交互式 ASP.NET 服务器端控件?
我很难理解如何合并 jQuery / 客户端 javascript 和 asp.net 以及整个页面生命周期模型。让我们举一个具体的例子。
我有以下 3 个列表,它们可以是网格视图、列表框或自定义控件。
List 1 - a list of search results
List 2 - an empty list
List 3 - an empty list
现在我希望能够将搜索结果从列表 1 拖动到列表 2 或 3,此外,我希望能够将搜索结果从列表 2 拖动到列表 3,或将列表 3 拖动到列表 2。
问题实际上并不在于这样做jQuery 的功能,问题出现在提交的时候,提交可以在你实际删除一个项目时完成,或者可能有一个专门的按钮来执行提交。
如果您只是进行回发,服务器端控件的视图状态将覆盖 jQuery 添加到客户端控件的值,并且当页面再次呈现时,一切都会被忘记。
如何将整个客户端与服务器端捆绑在一起?
一个可能的解决方案是将值存储在隐藏字段中,并让回发利用这个隐藏字段来恢复它所拥有的值,但我发现这个解决方案过于臃肿,考虑到在“过去”你只需向包含正确帖子数据的页面。
I'm having a hard time wrapping my head around how to merge jQuery / clientside javascript and asp.net and the whole page life cycle model. Let's take a concrete example.
I have the following 3 lists, they could be a gridview, a listbox or a custom control.
List 1 - a list of search results
List 2 - an empty list
List 3 - an empty list
Now I want the ability to drag a search result from List 1 into List 2 or 3, furthermore I'd like the ability to drag a search result from List 2 into 3, or List 3 into 2.
The problem is not actually making that functionality with jQuery, the problem comes when it is time to submit, a submit could be done when you actually drop an item, or there could be a dedicated button for performing the submit.
If you just do a postback, the viewstate of the serverside controls will override the values jQuery added to the controls on clientside and when the page renders again everything is forgotten.
How do you go by tying the entire clientside together with the serverside?
A possible solution would be to store the values in a hidden field and have the postback utilize this hidden field for restoring what values it had, but I find this solution bloated, considering that in the "old days" you would just make a post to a page with the correct post data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您的 DOM 中有状态,由 jQuery 脚本维护,但在与服务器的往返过程中您会丢失该状态。
要保留该状态信息,您必须将其放入隐藏字段(或 cookie,甚至 HTML5 离线存储)中,但更好的解决方案是完全避免回发并允许您的控件通过 AJAX 调用与服务器交互。这样,即使您与服务器交互,客户端状态也会保留。
The problem is that you have state in your DOM, maintained by your jQuery scripts, but you lose that state on a roundtrip to the server.
To retain that state information you would have to put it in hidden field (or cookie, or even HTML5 offline storage), but a better solution would be to avoid postbacks altogether and allow your controls to interact with the server via AJAX calls. Then your client-side state is retained even when you interact with the server.
您可以从您的控件注册客户端脚本
这可能会开始您的旅程:
http://weblogs .asp.net/asmith/pages/25465.aspx
You can register clientside script from your control
This may start you on your way:
http://weblogs.asp.net/asmith/pages/25465.aspx