Christian Vucossa programming blog

game programming, daily troubles and solutions

Posts Tagged ‘ajax’

ASP.Net Repeater updated with Ajax

Posted by Christian on March 21, 2009

Here is a simple method to update an aspnet Repeater calling a server method via ajax… here we go!

We make the ajax call with the favourite js framework or none at all. I use prototype anyway.

In our server method we do something like this:

List<Item> tobound = myDao.GetItems();

myRepeater.DataSource = tobound;

myRepeater.DataBind();

// and now the magic

// first of all we instance some usefull objects:

MemoryStream stream = new MemoryStream();

StreamWriter sw = new StreamWriter(stream);

HtmlTextWriter htw = new HtmlTextWriter(sw);

// then, we call the repeater’s own render method to render on our stream:

myRepeater.RenderControl(htw);

sw.Flush();

// ok, now we swap the stream in a byte array

Byte[] content = new Byte[stream.Length];

stream.Position = 0;

stream.Read(content, 0, stream.Length);

// and finally we transform the byte array in a string

string strContent = new System.Text.ASCIIEncoding().GetString(content);

// now strContent contains all the xhtml code that renders our repeater.

// Simply put it in the response and let it be consumed by the javascript callback

Page.Response.Write(strContent);

And our client code in the callback will look like this:

$(‘myrepeaterdiv’).update(transport.responseText);

Here we have a very simple method which doesn’t make use of any server side ajax component, just the old plain asp:Repeater and a good javascript ajax library.

Bye!

Posted in various | Tagged: , , , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.