Binding to ASMX always empty

5 posts, 1 answers
  1. Jocelyn
    Jocelyn avatar
    148 posts
    Member since:
    Jul 2012

    Posted 25 Jun 2014 Link to this post

    Hi,

    I am trying to bind a simple RadListView using a WebService (ASMX).

    Here is my code:
    <telerik:RadListView runat="server" ID="lvTest">
        <ClientSettings>
            <DataBinding>
                <ItemTemplate>
                    <div class="item">
                        #= Text#
                    </div>
                </ItemTemplate>
                <EmptyDataTemplate>
                    <div>No items</div>
                </EmptyDataTemplate>
                <DataService Location="~/Code/GenericHandlers/TestWS.asmx" DataPropertyName="Data" CountPropertyName="Count"
                    DataPath="GetData" SortParameterType="Linq" FilterParameterType="Linq" />
            </DataBinding>
        </ClientSettings>
    </telerik:RadListView>

    Here is my TestWS.asmx (Json() simply convert the object to a Json string using the JavascriptSerializer):
    <System.Web.Script.Services.ScriptService()> _
    <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
    <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <ToolboxItem(False)> _
    Public Class TestWS
        Inherits System.Web.Services.WebService
     
        <WebMethod()> _
        Public Function GetData() As String
            Return Json(New With {Key .Data = New Temp() {New Temp("Item1"), New Temp("Item2")}, .Count = 2})
        End Function
     
    End Class
     
    Public Class Temp
        Private _text As String
     
        Public Property Text() As String
            Get
                Return _text
            End Get
            Set(ByVal value As String)
                _text = value
            End Set
        End Property
     
        Public Sub New(ByVal pText As String)
            _text = pText
        End Sub
    End Class

    When I run the page, I can see in the Chrome debugger that GetData is correctly called and return this: 
    {"d":"{\"Data\":[{\"Text\":\"Item1\"},{\"Text\":\"Item2\"}],\"Count\":2}"}

    But I can't understand why the ListView is still empty and don't show the data.

    What did I miss there?

    Jocelyn
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 26 Jun 2014 in reply to Jocelyn Link to this post

    Hi Jocelyn,

    Please take a look into the ListView - Web Service Binding online demo and this help documentation for more information on how to configure the control in this case.

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jocelyn
    Jocelyn avatar
    148 posts
    Member since:
    Jul 2012

    Posted 26 Jun 2014 Link to this post

    Hi Shinu,

    I already looked at this help documentation and online demo. This is why I am posting here because it still doesn't work.

    This should be correctly coufigured because the ListView retrieve the Data from my asmx, but it doesn't to BIND it to the ListView.

    Thanks
  5. Answer
    Marin
    Admin
    Marin avatar
    1044 posts

    Posted 01 Jul 2014 Link to this post

    Hello Jocelyn,

    In order to correctly databind the RadListView the web service has to return data in JSON format. The returned data in your case is string which causes the problem:
    "d":"{\"Data\":[{\"Text\":\"Item1\"},{\"Text\":\"Item2\"}],\"Count\":2}"
    Additionally you also need a Layout template and an item placeholder where the ListView will render the data items (as shown in the demo)

    With this the correct setup of the RadListView and the asmx web service will look like this:
    <WebMethod> _
    <ScriptMethod(ResponseFormat := ResponseFormat.Json)> _
    Public Function GetData() As List(Of Temp)
        Dim list = New List(Of Temp)()
        list.Add(New Temp("Item1"))
        list.Add(New Temp("Item2"))
        Return list
    End Function

    <telerik:RadListView runat="server" ID="lvTest">
                    <LayoutTemplate>
                        <div id="items">
                        </div>
                    </LayoutTemplate>
                    <ClientSettings>
                        <DataBinding ItemPlaceHolderID="items">
                            <ItemTemplate>
                                <div class="item">
                                    #= Text#
                                </div>
                            </ItemTemplate>
                            <EmptyDataTemplate>
                                <div>No items</div>
                            </EmptyDataTemplate>
                            <DataService Location="TestWS.asmx" DataPath="GetData" />
                        </DataBinding>
                    </ClientSettings>
                </telerik:RadListView>



    Regards,
    Marin
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Jocelyn
    Jocelyn avatar
    148 posts
    Member since:
    Jul 2012

    Posted 02 Jul 2014 in reply to Marin Link to this post

    Hi Marin,

    Thanks for your help!

    This is what I was missing:
    <ScriptMethod(ResponseFormat := ResponseFormat.Json)> _

    Regards,
    Jocelyn
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017