数据绑定使用Ajax中继器 [英] Bind Data to Repeater using Ajax

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

问题描述

我想数据绑定到转发上点击一个特定的按钮

我能做到通过jQuery Ajax请求并调用该方法将数据绑定,但在页面上没有任何显示。

这是我使用的数据绑定到转发的方法:

 公共无效BindJobs()
{
    如果(RptClientDetails.Items.Count!= 0)回报;
    RptClientDetails.DataSource =新JobBusiness()GetJobInfoClient(客户端Id)。
    RptClientDetails.DataBind();
    回复于(myresponse);
    到Response.End();
}
 

上面的方法成功地调用和 GetJobInfoClient 检索到的数据。这是我的ajax调用:

 函数BindJobs(){
    $阿贾克斯({
        键入:POST,
        网址:客户端/ Default.aspx的行动= bindJobs
        数据: {},
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据类型:JSON,
        成功:函数(MSG){
            //做一些有趣的事情在这里。
        }
    });
}
 

解决方案

您要明白, RptClientDetails 是一个服务器端控件?当你在你的电话绑定到 BindJobs 则具有约束力,但不必返回生成的HTML。

您需要返回Repeater的HTML后,它已被绑定在你的AJAX。然后,在你的成功处理,将HTML注入您希望要显示转发的地方。

我会建议您检查出关于如何做到这一点里克施特拉尔的帖子:

jQuery和ASP.NET条第2部分

最简单的方法是放弃了AJAX一起,并使用的UpdatePanel 。只是包装你的转发和您的按钮的UpdatePanel 和通话将被框架内加以处理,并在新的HTML将被注入为您服务。它比做了jQuery / AJAX方法更重了一点,但它需要几乎为0 code。

例如:

 <! - 你的其他控件 - >
< ASP:UpdatePanel的ID =yourPanel=服务器>
    <的ContentTemplate>
        <! - 你的中继位置 - >
        <! - 您的按钮的位置和服务器端,使其拨打您绑定 - >
    < /的ContentTemplate>
< / ASP:UpdatePanel的>
<! - 其余的控制 - >
 

编辑:

如果你想呈现控件,以便您可以在您的响应发送回,你可以这样做:

  //这将让您的中继器呈现的HTML
StringBuilder的SB =新的StringBuilder();
StringWriter的TW =新的StringWriter(某人);
HtmlTextWriter的HW =新的HtmlTextWriter(TW);
RptClientDetails.RenderControl(HW);
字符串yourReatersRenderedHtml = sb.ToString();
 

您可以再返回该字符串中的AJAX调用,然后使用成功的方法将其注入到 DIV 或其他占位符。老实说,的UpdatePanel 要容易得多,我会建议,如果你在寻找这样做最简单的方式是不是真的担心使用<$ C对性能的影响$ C>的UpdatePanel 。

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

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

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();
}

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

解决方案

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.

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 would recommend checking out Rick Strahl's post regarding how to do this:

jQuery and ASP.NET Article Part 2

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

EDIT:

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();

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中继器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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