Christian Vucossa programming blog

game programming, daily troubles and solutions

Posts Tagged ‘c-sharp (c#)’

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 »

Windows Mobile and the “unknown publisher” exception

Posted by Christian on December 9, 2008

A few days ago I was deploying a new version of a software of mine on a PDA running Windows Mobile 6.0 and I have had to deal with a boring “unknown publisher” prompt. My new compiled dll was not recognized as trusted. What to do to get rid of this nuisance?

Well, I found this helpful link where the guy explains how to do but lacks of some important details so that I was obliged to surf again to fix. At the end of the story, I found a way.

To get rid of the message we have to edit a registry key. My app was in .NET (C#), and here is the code that fix the problem:

string keyname = “0000101a”;
RegistryKey securityKey = Registry.LocalMachine.OpenSubKey(“Security”)
.OpenSubKey(“Policies”).OpenSubKey(“Policies”, true);
object val = securityKey.GetValue(keyname);
securityKey.SetValue(keyname, 1, RegistryValueKind.DWord);

Hope this helps.

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

 
Follow

Get every new post delivered to your Inbox.