Wensveen's Blog

March 28, 2012

Typing Danish characters on a US keyboard

Filed under: Uncategorized — Tags: , , , , , , , , — wensveen @ 11:35 am

I live in Denmark, and because I am learning Danish I often have to type the Danish characters æ, ø and å. Since I still have a US keyboard, this is not easy. At least on windows. I usually open the Character Map application and select the characters from there. Once you have inserted them in a text you can copy-paste them when you need them. This works, but it is a little cumbersome.

Character Map

Another option is to hold the Alt key and type the character’s ASCII code on the NumPad, but that means remembering 6 rather random numbers. Here is a table, if you like:

Character NumPad (ASCII) Code
Æ 198
æ 230
Ø 216
ø 248
Å 197
å 229

Unfortunately, many laptops don’t have a NumPad to save space. So not an option.

Luckily, there is a cool program from Microsoft called The Microsoft Keyboard Layout Creator which you can use to create your own keyboard layouts, based on existing ones or from scratch.
(more…)

May 27, 2011

Toggling boilerplate HTML visibility in ASP.NET

Filed under: Uncategorized — Tags: , — wensveen @ 10:23 pm

The problem

When you are writing an aspx page, you write HTML mixed with ASP.NET controls. Sometimes, when some control is not visible, or has no visible (html) output, you may need to hide the html that is containing the control as well.

For example, suppose a document from some CMS may or may not have a Title, or said Title may be empty. Your aspx page might look like this:

..
<h1><cms:FieldControl runat="server" Field="Title" /></h1>
..

Suppose FieldControl is a control that renders the a field of the current document when that field exists and isn’t empty. When Title is empty, you don’t want the <h1> tag to be rendered either.

Solution 1: Placeholders and code-behind

The most straightforward solution to this problem is to place an <asp:Placeholder> control around the <h1>. Then in code-behind, test the FieldControl for output and set the Placeholder visibility accordingly. For example:

<asp:Placeholder runat="server" ID="phTitle">
	<h1><cms:FieldControl runat="server" ID="fcTitle" Field="Title" /></h1>
</asp:Placeholder>

code behind:

protected override void OnPreRender(EventArgs e)
{
	base.OnPreRender(e);
	phTitle.Visible = fcTitle.Visible && !String.IsNullOrEmpty(fcTitle.Text)
}

While there is nothing wrong with this code, it could become cumbersome if you have more than a few of these snippets. There may even be controls that don’t expose a Text property or a similar way to test the control for output.

Solution 2: Introducing the Enclosure control

(more…)

April 12, 2011

A Fluent interface for creating Control hierarchies (method chaining)

Filed under: Uncategorized — Tags: , — wensveen @ 8:34 am

I was creating a custom ASP.NET control for a project and I noticed that creating Control hierarchies was a lot of work, and a bit clunky. For instance, create a table with a single row, with a single cell with just two Panels and some literals in them.

Control structure:

  • (this)
    • Table
      • TableRow
        • TableCell
          • Panel
            • Literal
          • Panel
            • Literal

Table table = new Table { BorderStyle = BorderStyle.None, Width = Unit.Percentage(100) };
TableRow tr = new TableRow();
TableCell td = new TableCell();

Panel ush = new Panel { CssClass = "UserSectionHead" };
ush.Controls.Add(new Literal { Text = "Title" });
td.Controls.Add(ush);

Panel usb = new Panel { CssClass = "UserSectionBody" };
usb.Controls.Add(new TextField { Text = "Enter title" });
td.Controls.Add(usb);

tr.Cells.Add(td);
table.Rows.Add(tr);
this.Controls.Add(table);

As you can see, there’s a lot of variables you need to create just to be able to add (nested) child controls to the control hierarchy. And this is just a very small example. Fortunately, most properties can be set in the initializer, otherwise the boilerplate would be even larger!

(more…)

March 1, 2010

Currying in C#

Filed under: Uncategorized — Tags: , — wensveen @ 4:06 pm

A few days ago, I suddenly felt like experimenting with currying in C#. For those of you that don’t know what currying is, from Wikipedia currying: “[..] currying [..] is the technique of transforming a function that takes multiple arguments [..] in such a way that it can be called as a chain of functions each with a single argument.”

A simple example:

int Add(int x, int y) { return x + y; }
add3 = Add(3);

r1 = add3(6);
r2 = add3(9);

(more…)

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.