使用Ajax将数据绑定到Repeater [英] Bind Data to Repeater using Ajax

查看:129
本文介绍了使用Ajax将数据绑定到Repeater的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据绑定到中继器,点击特定的按钮

I want to Bind data to a Repeater on click of a particular Button.

我可以通过jQuery执行ajax请求,并调用方法来绑定数据,但在页面上没有显示任何内容。

I can do an ajax request via jQuery and call the method to bind the data, but on the page nothing is displayed.

是我用于将数据绑定到 Repeater 的方法:

This is the method I use to bind the data to the Repeater:

public void BindJobs()
{
    if (RptClientDetails.Items.Count != 0) return;
    RptClientDetails.DataSource = new JobBusiness().GetJobInfoClient(ClientId);
    RptClientDetails.DataBind();
    Response.Write("myresponse");
    Response.End();
}

上述方法被成功调用,并且由 GetJobInfoClient 。这是我的ajax电话:

The above method is successfully called and the data retrieved by GetJobInfoClient. This is my ajax call:

function BindJobs() {
    $.ajax({
        type: "POST",
        url: "Client/Default.aspx?action=bindJobs",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            // Do something interesting here.
        }
    });
}


推荐答案

你意识到 RptClientDetails 是服务器端控件?当您绑定到 BindJobs 的绑定时,它是绑定的,但您不返回生成的HTML。

You do realize that RptClientDetails is a server side control? When you are binding to it in your call to BindJobs it is binding but you are not returning the generated HTML.

您需要在中继器的HTML被绑定到AJAX后返回。然后在您的成功处理程序中,将HTML注入到您想要显示 Repeater 的位置。

You need to return the Repeater's HTML after it has been bound in your AJAX. Then in your success handler, inject the HTML into the place where you want the Repeater to be displayed.

I建议您查看 Rick Strahl's 关于如何执行此操作的信息:

I would recommend checking out Rick Strahl's post regarding how to do this:

jQuery和ASP.NET文章第2部分

jQuery and ASP.NET Article Part 2

最简单的方法是将AJAX放在一起,并使用 UpdatePanel 。只需将 Repeater 和您的按钮 UpdatePanel 该调用将由框架处理,并为您注入新的HTML。这比使用jQuery / AJAX方法要重一些,但是它需要几乎0个代码。

The easiest way would be to drop the AJAX all together and use an UpdatePanel. Just wrap your Repeater and your Button with an UpdatePanel and the call will be handled by the framework and the new HTML will be injected for you. It's a little heavier than doing it the jQuery/AJAX method but it takes almost 0 code.

EG:

<!-- Your other controls -->
<asp:UpdatePanel ID="yourPanel" runat="server">
    <ContentTemplate>
        <!-- Your Repeater HERE -->
        <!-- Your Button HERE and server side make it call your bind -->
    </ContentTemplate>
</asp:UpdatePanel>
<!-- rest of your controls -->

编辑:

如果要渲染控件,以便您可以在回复中发送回来,您可以执行以下操作:

If you want to render the control so that you can send it back in your response you can do:

// This will get your Repeaters rendered HTML
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
RptClientDetails.RenderControl(hw);
string yourReatersRenderedHtml = sb.ToString();

然后,您可以在AJAX调用中返回此字符串,然后使用成功方法将其注入到 div 或其他一些占位符。老实说, UpdatePanel 更容易,如果您寻找最简单的方法,我建议使用它,而不是非常担心使用 UpdatePanel

You could then return this string in the AJAX call and then use the success method to inject it into a div or some other place holder. Honestly, the UpdatePanel is far easier and I would recommend it if your looking for the easiest way to do this and are not really worried about the performance impact of using an UpdatePanel.

这篇关于使用Ajax将数据绑定到Repeater的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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