ASP.NET:使用 jQuery 绑定中继器?

发布于 2024-07-23 23:00:27 字数 334 浏览 2 评论 0原文

我有一个绑定服务器端的中继器控件。 它重复一系列的 div,并且这样做没有问题。 我有一些按钮用于对中继器进行排序(最新的、排名最高的、随机的),这按其应有的方式工作。

我想通过以某种方式使用 Ajax/jQuery 对按钮进行排序来改善我的用户体验,这样就不会出现页面回发,并且用户不会失去他/她在页面上的位置。

有没有办法使用 jQuery 访问这样的服务器端代码,或者使用 Ajax 重新绑定服务器端控件?

谢谢...如果我需要列出更多详细信息,请告诉我!

编辑我知道UpdatePanels,但如果不需要,我宁愿不使用它们。

I have a Repeater control that I bind server-side. It repeats a series of divs, and does so with no problem. I have some buttons that I use to sort the repeater (newest, highest ranked, random) and this works the way it should.

I would like to improve my user experience by making the buttons sort the divs using Ajax/jQuery somehow so that there is no page postback and the user does not lose his/her spot on the page.

Is there a way to use jQuery to access server-side code like this, or use Ajax to re-bind a server-side control?

Thanks... if I need to list more details, please let me know!

EDIT I'm aware of UpdatePanels, but I would prefer not to use them if I don't have to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痴骨ら 2024-07-30 23:00:27

这相对容易。

  1. 将中继器移至单独的自定义控件,例如 MyControl。 现在,页面中的转发器变为 uc1:MyControl。

  2. 将 MyControl 包装到 div 中:

  3. 创建一个仅包含 MyControl 的新页面 pgMyControl.aspx。
  4. 在主页上,将 jQuery 处理程序添加到排序链接。 使用 load 方法动态替换 div 内容:

    $('#link_sort_random').click(function() 
      { 
        $("#mydiv").load("pgMyControl.aspx&sort=random"); 
      } 
      
  5. 在 MyControl 内的数据源中使用 QueryStringParameter 来更改顺序。 或者在代码隐藏文件中使用Request.QueryString。

It's relatively easy.

  1. Move your repeater to a separate custom control, let's say MyControl. Now repeater in your page becomes uc1:MyControl.

  2. Wrap MyControl into a div:

    <div id="mydiv">
      <uc1:MyControl ID="MyControl1" runat="server" />
    </div>
    
  3. Create a new page, pgMyControl.aspx, that contains MyControl only.
  4. On your main page, add jQuery handlers to your sort links. Use load method to dynamically replace div contents:

    $('#link_sort_random').click(function()
    {
      $("#mydiv").load("pgMyControl.aspx&sort=random");
    }
    
  5. Use QueryStringParameter in datasource inside MyControl to change order. Or use Request.QueryString in code-behind file.
沉默的熊 2024-07-30 23:00:27

您是否考虑过 将中继器的功能移至客户端

这样做, 功能像分页和排序并不是很难添加。 事实上,您可以通过使用 ADO.NET 数据服务< /a> 作为服务层。

Have you considered moving the Repeater's functionality to the client-side?

Doing it that way, functionality like paging and sorting is not very difficult to add. In fact, you can lean on the framework pretty heavily by using ADO.NET data services as the service layer.

ぶ宁プ宁ぶ 2024-07-30 23:00:27

使用 updatePanel 或 jquery Ajax 回发本质上是同一件事。 两者都会要求您的代码获取新查询,然后让您的控件自行渲染,然后将 HTML 作为部分页面渲染反馈给客户端,然后将内容插入到同一 DOM 位置中的旧内容中。

由于 Web 控件的性质及其决定何时呈现的生命周期,以这种方式使 JQuery 和 ASP.NET 相互通信要困难得多。 updatePanel 知道如何调用所有这些,维护正确的视图状态并将结果返回到正确的位置。

在这种情况下,不要让事情变得更加困难,请使用 updatePanel,除非您有一些非常具体的理由不这样做。

编辑:如果您在更新面板时遇到 JQuery 问题,可能是由于创建了新的 DOM 节点。 JQuery 有 live 事件 来处理这个问题。 即使在文档准备好之后,它也会注意到何时创建新的 DOM 元素并将它们与您的选择器进行匹配。

Using an updatePanel or a jquery Ajax postback are the same thing essentially. Both will ask your code to fetch the new query, then make your control render itself, and then feed the HTML back to the client as a partial page render, and then insert the content in place of the old content in the same DOM location.

It is considerably harder to make JQuery and ASP.NET talk to each other this way due to the nature of web controls and their lifecycle that determines when they render. An updatePanel knows how to call all this, maintain proper viewstate and return the result to the correct location.

In this case, don't make things any harder on yourself, use the updatePanel unless you have some very specific reason not to.

EDIT: If you're having JQuery issues with update panels it is probably due to the fact that new DOM nodes being created. JQuery has the live event to handle this. It will notice when new DOM elements are created and match them against your selector even after the document ready.

浪荡不羁 2024-07-30 23:00:27

也许这是一个 OT,但您可以考虑更改绑定客户端和服务器控件的方式,使用 XSLT 转换而不是经典服务器控件。
您可以在此处找到示例(抱歉,是意大利语......)。

Maybe it's an OT, but you can consider to change the way you bind even the client and the server control, using XSLT transformation instead od the classics server controls.
You can find an example here (sorry, it's in italian...).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文