Ajax.NET and DataBound Server Controls

Over on the AjaxPro Google Group, a user asked a question about databinding a repeater using Ajax.  The response has always been "A repeater (or any other server control) is rendered on the server, and then passed back to the client as HTML."  One of the purposes of Ajax is to reduce the amount of bytes being sent back and forth between server and client on non-initial load functions.

In this post, the usual answers were given.  Loop through the dataset with JavaScript, then display the results.  This makes sense to me.

Then I got thinking.  For those who don't want the hassle of looping through datasets to build tables, but are more comfortable with server side databinding, there is a way...

***SERVER SIDE***

<AjaxPro.AjaxMethod()> _
    
Public Function getDG() As String

        Dim 
ds As New DataSet
        
'Fill the DS however you want...
        
        
Dim DataGrid1 As New DataGrid
        DataGrid1.DataSource = ds
        DataGrid1.DataBind()

        
Dim SB As New System.Text.StringBuilder
        
Dim SW As New System.io.StringWriter(SB)
        
Dim htmlTW As New HtmlTextWriter(SW)

        DataGrid1.RenderControl(htmlTW)

        
Return SB.ToString

    
End Function

***CLIENT SIDE***

<div id="DataGrid1"></div>
<
href="javascript:void(0);" onclick="test();">Here</a>
<
script>
    
function test() 
        {
        ServerSideRenderControl.WebForm1.getDG(getDGCallback);
        }
    function getDGCallback(res)
        {
        document.getElementById("DataGrid1").innerHTML = res.value;
        }
</script>

This method (though large) allows you to databind a data-aware control on the server side, and then pass it out to the client through Ajax.  I haven't dealt with server-side templates (for repeaters)...  You're on your own for that.  Enjoy!

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
Print | posted on Wednesday, February 22, 2006 8:28 AM

Feedback

# re: Ajax.NET and DataBound Server Controls

left by hoangkim at 1/2/2007 9:53 PM Gravatar
I've just tried to your code. But myproject not run. Error serverSideRender is not. If I just use WebForm1.getDG(...), it don't understand function getDG() in code behide page/ Can you help me? Thank you !

# re: Ajax.NET and DataBound Server Controls

left by SivaL at 1/3/2007 11:11 PM Gravatar
wow, its working fine, Do you have any source code for paging using AJAX..
Thanks in Advance

# re: Ajax.NET and DataBound Server Controls

left by ctripp at 7/20/2007 8:12 AM Gravatar
Where does ServerSideRenderControl get defined/come from?
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: