How to bind data in Repeater in Json

Aypn CNN 446 Reputation points
2023-05-08T16:32:23.7533333+00:00

Hi,

Referring to the below codes, I'm getting values properly, but can't bind values in Repeater, Where is the problem? how to fix it.

Aspx.VB Page

sCmd.Parameters.Add(param)
            dtadp.Fill(dtset)
            If sCmd.Parameters("@o_ErrorStatus").Value = 1 Then
                f_retval = dtset.Tables(0)
            ElseIf sCmd.Parameters("@o_ErrorStatus").Value = 0 Then
                f_retval = New DataTable()
            End If
        Catch ex As Exception
            f_retval = dtset.Tables(0)
        Finally
            sCon.Close()
        End Try
        Return JsonConvert.SerializeObject(f_retval)

-------------------
Aspx page

$.ajax({
                type: "POST",
                url: "QueryList.aspx/getQryInfo",
                data: '{fGet: ' + JSON.stringify(fGet) + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    var result = JSON.parse(response.d);
                    $.each(result, function (i, item) {
                        var hoquery = item.HOQueries;
                        var branchreply = item.BranchResponse;
                      $('#rptUserinfo').append('<tr><td>' + hoquery + '</td><td>' + branchreply + '</td></tr>');
                    });
                },
                error: function (data, ex) {
      
---------------

Repeater 

<asp:Repeater ID="rptUserinfo" runat="server">
                                                            <HeaderTemplate>
                                                                <table id="tblQryInfo" class="table">
                                                                    <tr>
                                                                        <th id="thHO" runat="server">HO Query</th>
                                                                        <th id="thBrReply" runat="server">BranchReply</th>
                                                                    </tr>
                                                            </HeaderTemplate>
                                                            <ItemTemplate>
                                                                <tr style="font-size: 10px;">
                                                                    <td id="tdho" runat="server"><%#Eval("hoquery") %></td>
                                                                    <td id="tdbr" runat="server"><%#Eval("branchreply") %></td>
                                                                </tr>
                                                            </ItemTemplate>
                                                            <FooterTemplate>
                                                                </table>
                                                            </FooterTemplate>
                                                        </asp:Repeater>


ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,507 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 29,751 Reputation points Microsoft Vendor
    2023-05-09T06:33:55.3366667+00:00

    Hi @Aypn CNN,

    You need to put the Repeater control inside an HTML DIV because the Repeater control doesn't render itself, so you can't access it via JavaScript or jQuery using the client ID.

    Here is the changed code:

    <div id="dvTests">
                <asp:Repeater ID="rptUserinfo" runat="server">
                  //.......
                </asp:Repeater>
            </div>
    
    $('#dvTests table').append('<tr><td>' + hoquery + '</td><td>' + branchreply + '</td></tr>');
    

    You also need to make sure you get the correct json, which you can check with alert(result);

    The following is an example of my test using test data, you can refer to:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-3.4.1.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $.ajax({
                    type: "POST",
                    url: "WebForm3.aspx/getQryInfo",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        var result = JSON.parse(response.d);
                        alert(result);
                        $.each(result, function (i, item) {
                            var hoquery = item.HOQueries;
                            var branchreply = item.BranchResponse;
                            $('#dvTests table').append('<tr><td>' + hoquery + '</td><td>' + branchreply + '</td></tr>');
                        });
                    },
                    error: function (data, ex) { }
                });
    
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div id="dvTests">
                <asp:Repeater ID="rptUserinfo" runat="server">
                    <HeaderTemplate>
                        <table id="tblQryInfo" class="table">
                            <tr>
                                <th id="thHO" runat="server">HO Query</th>
                                <th id="thBrReply" runat="server">BranchReply</th>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr style="font-size: 10px;">
                            <td id="tdho" runat="server"><%#Eval("HOQueries") %></td>
                            <td id="tdbr" runat="server"><%#Eval("BranchResponse") %></td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>
            </div>
        </form>
    </body>
    </html>
    
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim tests As List(Of Test) = New List(Of Test)()
            tests.Add(New Test())
            rptUserinfo.DataSource = tests
            rptUserinfo.DataBind()
    
        End Sub
    
        <WebMethod>
        Public Shared Function getQryInfo() As String
            Dim p As List(Of Test) = New List(Of Test)()
            Dim p1 As Test = New Test()
            p1.Id = "1"
            p1.HOQueries = "a"
            p1.BranchResponse = "a"
            Dim p2 As Test = New Test()
            p2.Id = "2"
            p2.HOQueries = "b"
            p2.BranchResponse = "b"
            p.Add(p1)
            p.Add(p2)
            Dim json As String = JsonConvert.SerializeObject(p)
            Return json
        End Function
        Public Class Test
            Public Property Id As String
            Public Property HOQueries As String
            Public Property BranchResponse As String
        End Class
    

    User's image

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.