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>
<a 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!
Print | posted on Wednesday, February 22, 2006 8:28 AM