Christian Vucossa programming blog

game programming, daily troubles and solutions

Archive for the ‘various’ Category

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 »

DataGridView Cell’s editing: handle user’s input

Posted by Christian on January 27, 2009

Today I faced an issue that stole me a couple of hours of lazy work… My need was to intercept all the ‘.’ chars inputed by the user on a DataGridView cell and replace them with commas. With my big surprise, I have not found any suitable event of method.

After a coffebreak, I returned with clear mind and restored energies at the problem and I found this event on the DataGridView object:

EditingControlShowing

Neither the name nor the help tooltip of the intellisense help me so much understanding its behavior, but I registered to it and took a try.

Its DataGridViewEditingControlShowingEventArgs provide me with a Control object that is exactly the control used by the cell to handle input, a TextBox in my case. And now the magic.

I tried to treat the control as a normal TextBox, registering to its KeyPress event and it works!

myGrid.EditingControlShowing+=new DataGridViewEditingControlShowingEventHandler(myGrid_EditingControlShowing);

private void myGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
if (e.Control is TextBox) {
((TextBox)e.Control).KeyPress +=new KeyPressEventHandler(HandleKeyPress);
}
}

private void HandleKeyPress(object sender, KeyPressEventArgs e) {
// My stuff here…
}

Another day at work, another .NET  trick learned…

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

Happy new year

Posted by Christian on December 30, 2008

Time to spend some days far from computers. I’m just back from mountain, as the picture shows. Here I am in Campo Carlo Magno, Madonna di Campiglio (Italy) on Dolomiti. Beautiful place and beautiful snow.

mont2008

But now I’m back, and my projects are waiting…

Posted in various | Tagged: | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.