ASP.NET/DataList:通过单击列表元素进行客户端回发
我有一个显示一组元素的 DataList 控件。 任何人都可以为我指明正确的方向,告诉我如何添加一些客户端功能,以便在用户单击列表中的元素时提交/进行回发(例如,列表元素根目录中的任何位置)。我已经看到了一些通过添加隐藏的 LinkButton 并将其连接起来的示例 - 但我还没有让它正常工作,
欢呼,
--larsw
感谢您的帮助 - 我使用了 jQuery 方法,你知道我是否可以调用隐藏的方法吗? asp:LinkButton 来自 lambda 函数(针对所选项目),以便进行回发?
您的代码示例中存在一个小拼写错误(以防其他人阅读此线程): 我必须向 jQuery 选择器添加 #; '#<%= DataList1.ClientID %> td'
--larsw
I have a DataList control that displays a set of elements. Can anyone point me in the right direction on how I can add some client-side functionality for submitting/doing a postback when the user clicks an element in the list (e.g. anywhere in the that is the root of the list element. I've seen some examples by adding a hidden LinkButton and wiring it up - but I haven't got it to work properly.
cheers,
--larsw
Thanks for the help - I went for the jQuery method. Do you know if I can invoke a hidden asp:LinkButton from the lambda function (for the selected item) so that a post back takes place?
There was a small typo in your code example (in case anyone else reads this thread):
I had to add a # to the jQuery selector; '#<%= DataList1.ClientID %> td'
--larsw
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不存在“客户端回发”这样的东西,这是一个错误的声明。 回发意味着提交到服务器(或外部服务器)。
您真正需要的是向页面添加一些 AJAX 方法。 这可以通过几种方式完成:
UpdatePanel 方法
这实际上不是如果您的页面很重,这是最好的主意。 如果您想了解有关注意事项的更多信息,请查看我写的博客文章 - http://www.aaron-powell.com/blog.aspx?id=1195。
简而言之,如果您不了解其限制是什么,那么 UpdatePanel 可能是一个危险的选择。
MS AJAX 和/或 jQuery
这是我对您应该做什么的建议。 使用 jQuery 定位 DOM 中要放置客户端事件的所有元素,例如:
David Ward 有一些关于在 ASP.NET/ASP.NET AJAX 中使用 jQuery 的好文章 - http://encosia.com/2008/03/27/using -jquery-to-consume-aspnet-json-web-services/ 和 http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
注意:如果您要使用 AJAX 实现,您将不会访问页面的控件集合,这将全部是静态方法交互,因此请注意,如果您如果想要更新页面的多个部分,您需要编写 JavaScript 方法来执行此操作。
There isn't such a thing as a "client side postback", it's an erronous statement. A postback implies submission to the server (or an external server).
What you're really looking for is adding some AJAX methods to your page. This can be done in a few ways:
UpdatePanel Method
This isn't really the best idea if you've got a very heavy page. Have a look at a blog post I wrote if you want to get some more info on what to look out for - http://www.aaron-powell.com/blog.aspx?id=1195.
Simply put UpdatePanels can be a dangerous choice if you don't understand what the limitations are.
MS AJAX and/ or jQuery
This is my recommendation on what you should do. Use jQuery to locate all the elements in the DOM that you want to put the client events on, for example:
David Ward has some good posts on using jQuery with ASP.NET/ ASP.NET AJAX - http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/ and http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
Note: If you're going to use an AJAX implementation you wont have access to the page's control collection, it will all be static method interaction so be aware that if you want to update multiple sections of the page you'll need to write JavaScript methods to do that.