webcam porn

New version of the Silverlight Calendar

Friday, October 3rd, 2008 | .NET | 2 Comments

Some time ago, I wrote a birthday calendar in Silverlight 1.0. It was purely JavaScript and somewhat restricted because of that. Later I upgraded it to Silverlight 1.1, the first version of Silverlight that used managed code. Silverlight 1.1 is now Silverlight 2.0, but there has been a lot of changes between those version.
So here is the latest version compatible With Silverlight 2.0 Beta 2. I also added Tim Rule's excellent glowing and shadow effects, which are just awesome!

Download it here.


Moving to WordPress

Thursday, September 18th, 2008 | General | 1 Comment

I have been using Mambo/Joomla for quite some time and have realized that it's not ideal for blogging. I decided to go with a real blogging engine, like WordPress. It's really neat and easy to use and has a lot of plug-ins and features. I migrated all my joomla content with a script and it worked pretty well. Had to upload and fix all the images manually, and all the comments are missing, but what the heck, I am pretty satisfied.

My next move will be to create my own personal skin for WP, even though I like the one I am using. I just have to put this in the queue for all of my other projects and time consuming things.

Hope you like the new blog, at least I do. The old blog is still active and will be removed pretty soon. Redirects should work for most articles, so old links should be intact. Thanks to the redirection plug-in for WordPress :)

Html to Pdf in .NET

Thursday, August 14th, 2008 | .NET | 88 Comments

I searched around for some OpenSource projects converting HTML to PDF, and stumbled upon a great library called iTextSharp . It's actually a Java library ported to .NET (typically isn't it?), and it has some really nice features. There are some good commercial products out there doing the same, but in my opinion this is core functionality, so if one can get it for free and even get the source, nothing is better than that!

Regarding PDF creation, iTextSharps main function is generating PDF from scratch, not converting them from HTML. The library has a really understandable API for developers that are not familiar with the PDF specification, me beeing one of them.

Playing around with the API and reading some news lists and forum posts, I finally managed to get a working sample on how to export a GridView to PDF. This is a pretty simple sample, but you can play around with the API to add more formatting and do your stuff. Actually you can export anything in the XHTML, providing the markup is legal. However, not all html tags are supported. The intention of the author was not to make a HTML2PDF converter, but more like create PDF's from HTML if the markup supports the engine. So you will probably not be able to convert dynamic content you do not have control over, but it's excellent for creating reports etc. Supported tags are: "ol ul li a pre font span br p div body table td th tr i b u sub sup em strong s strike h1 h2 h3 h4 h5 h6 img"

So how does it work, then? Well, first you need to get the latest version of iTextSharp. Just place the dll to your bin folder in your Web Application Project (you're not using Web Projects, are you ;-) ) and add a reference to it. Then create a new page, add a PlaceHolder to it, The placeholder will be the section of the HTML that you will export to PDF. You can add some more controls to the placeholder if you need. Inside the placeholder add a GridView and bind it to your datasource.

Add a ASP:Button to the page. This will trigger the export. The code when the button is clicked is doing all the exporting stuff:

protected void ButtonCreatePdf_Click(object sender, EventArgs e)


//Set content type in response stream

Response.ContentType = "application/pdf";

Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");


//Render PlaceHolder to temporary stream

System.IO.StringWriter stringWrite = new StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);


StringReader reader = new StringReader(stringWrite.ToString());

//Create PDF document

Document doc = new Document(PageSize.A4);

HTMLWorker parser = new HTMLWorker(doc);

PdfWriter.GetInstance(doc, Response.OutputStream);




//Create a footer that will display page number

HeaderFooter footer = new HeaderFooter(new Phrase("This is page: "), true)

{ Border = Rectangle.NO_BORDER };

doc.Footer = footer;

//Parse Html



catch (Exception ex)


//Display parser errors in PDF.

//Parser errors will also be wisible in Debug.Output window in VS

Paragraph paragraph = new Paragraph("Error! " + ex.Message);


Chunk text = paragraph.Chunks[0] as Chunk;

if (text != null)


text.Font.Color = Color.RED;









Almost there. Clicking the button will result in an exception:

Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

This is because we are trying to render the PlaceHolder control in a stream and not in a WebForm. A neat .NET security feature to prevent Injection attacks. This exception is simply ignored by adding the following code to the page:

public override void VerifyRenderingInServerForm(Control control)



Now, clicking the button again results in another Exception:

RegisterForEventValidation can only be called during Render();

Again, a security feature of .NET. Ignore this excpetion by setting EnableEventValidation="False" in the Page header. If you are concerned about the security of the page, please check official documentation of the features that has been disabled for the page.

Now, clicking the button should result in a PDF document. If not, debug the application and check Debut Output window for exceptions. Probably the XHTML in the placeholder is not valid.

Note that I am using HTMLWorker class instead of the HtmlParser class in the iTextSharp library. According the the author, the HTMLParser is not supported. I tried both, and the HTMLWorker swallows a lot more HTML markup than the HtmlParser.

You probably also want to clean the HTML before parsing it with the HTMLWorker. Typically you want to remove javascript postbacks, anchors etc. from the GridView. This can be achieved with the following code:

string html = stringWrite.ToString();

html = Regex.Replace(html, "</?(a|A).*?>", "");

StringReader reader = new StringReader(html);

You can also extend the HTMLWorker class making it more specialized for your purpose. For instance it would be great to be able to define pagebreaks in the final PDF document. Simply create a new class inherited from HTMLWorker.

public class HTMLWorkerExtended : HTMLWorker


public HTMLWorkerExtended(IDocListener document) : base(document)


public override void StartElement(String tag, Hashtable h)


if (tag.Equals("newpage"))



base.StartElement(tag, h);



Now, simply replace the HTMLWorker with the extended version and add a <newpage /> element to the HTML in the placeholder where you want a new page.

There are some css styles not supported by default. For instance it is not possible to set the background-color style for an image or tablecell/-row. The only solution for adding more style support is changing the iTextSharp source. It's pretty simple, however. Open \text\html\simpleparser\FactoryProperties.cs. In the InsertStyle method add the following code to the foreach loop:

else if (key.Equals(Markup.CSS_KEY_BGCOLOR))


Color c = Markup.DecodeColor(prop[key]);

if (c != null)


int hh = c.ToArgb() & 0xffffff;

String hs = "#" + hh.ToString("X06", NumberFormatInfo.InvariantInfo);

h["bgcolor"] = hs;



Update: Another example for adding border color to a table:

First, set the border style for the table, ie. style="border-color: #ff0000;"

Then again, you need to apply the style to FactoryProperties.cs file as in the example above.

else if (key.Equals(Markup.CSS_KEY_BORDERCOLOR))
    Color c = Markup.DecodeColor(prop[key]);
    if (c != null)
        int hh = c.ToArgb() & 0xffffff;
        String hs = "#" + hh.ToString("X06", NumberFormatInfo.InvariantInfo);
        h["border-color"] = hs;

In addition you need to alter the output of the table as there is no default "bordercolor" style property in the IncTable class.

Open IncTable.cs and change the following code in the BuildTable method:
Existing code:

for (int row = 0; row < rows.Count; ++row) {
    ArrayList col = (ArrayList)rows[row];
    for (int k = 0; k < col.Count; ++k) {

Replace with:

String bordercolor = (String)props["border-color"];
for (int row = 0; row < rows.Count; ++row)
    ArrayList col = (ArrayList)rows[row];
    for (int k = 0; k < col.Count; ++k)
        PdfPCell cell = (PdfPCell)col[k];
        cell.BorderColor = Markup.DecodeColor(bordercolor);

This will change the border color on the cell in the table. Hint: It could be wise to check if the cell already has a border color before overwriting it with the table border color.

Recompile and add the new DLL to your project.

Happy Coding!


Using the RegularExpressionValidator for validating length of text

Thursday, May 22nd, 2008 | .NET | 5 Comments

The RegularExpressionValidator can be used for almost everything regarding validation of input fields. My favourites are validating Email, Dates and length of text in textareas. The only problem with the length validation, is validating text that includes newline. One would think that the regex: .{0,4000} would be an easy way to validate maximum length of 4000 characters. That is true, but the dot does not include NewLine (CheatSheet). The RegularExpressionValidator also does NOT have an option for parsing the text as single line, which is a common Regular Expression option.

Reading another blog I found an expression that includes newlines: ^(.|\s){0,n)$
The only problem with this is that IE 6/7 and FF freezes when trying to test the expression. Seems like this is a JavaScript/ECMAScript problem. Disabling the clientscript for the RegularExpressionValidator however would probably solve this. The expression works fine in RegexCoach.

A comment in the blog suggested in using:  ^[\s\S]{0,n}$
This expression seems to work fine in the browser. Also seems to work fine with number of characters. So I think I will stick with that for now.


Silverlight 2.0 Beta is here!

Thursday, March 6th, 2008 | .NET | 1 Comment

Finally, the first beta version of Silverlight 2.0 is here! You can download it at :

Download it here.

This download includes the plugin, the SDK and the Visual Studio 2008 tools.
Rumours say that the final release will be in September, but we will just wait and see…

You would also want to download Microsoft Expression Blend 2.5 Preview which has support for Silverlight 2

Just waiting for Scott Guthrie to start blogging about the details :)

› Continue reading


SilverligthCalendar [updated]

Tuesday, January 29th, 2008 | .NET | No Comments

The SilverlightCalendar is a Silverlight (obviously) application that I wrote back in 2007. I was originally written for Silverlight 1.0, but I have now upgraded it for Silverlight 1.1. The main features are the same:

  • All the calendar data is fetched from an SQL server, using vanilla ADO.NET and webservices.
  • All layout and controls are dynamically added to the root canvas. No hardcoded layout at all.
  • Heavy use of animations, using storyboards.
  • Everything is event based.
  • Supports zindex for focusing objects

Additionally, the 1.1 version has the following new features:

  • Much better use of custom controls and XAML. Use Expression Blend 2 (preview) to update XAML.
  • Fullscreen support
  • Resize support
  • Additional fonts support

The interesting part in my opinion, is the integration with the business layer. This is usefull in real world scenarios, fetching data from data repositories or services and using the data in the Silverlight application.

SilverlightCalendar 1.0

The Silverlight application is written 100% in JavaScript. It is basiclly production ready, but it's hard to maintenance. The XAML is escaped in JavaScript which makes it incompatible with Expression Blend for instance. It is possible to extract the XAML and use a downloader object to make it less integrated in the JavaScript, so you are welcome to modify it if you like ;)


  • Visual Studio 2005

Download the source code. Includes the database as well (SQLEXPRESS).

SilverlightCalendar 1.1

The 1.1 version is written in C# and is much more flexible and much easier to maintain. This is how real Silverlight applications should be written. Each Silverlight control is a custom control and can be modified in Expression Blend 2. That makes it really much easier to create the XAML and handle state in the control. In addition, a new set of tools were used to create it, making it easier to use and maintain.


  • Visual Studio 2008
  • Silverlight 1.1 Alpha refresh SDK september
  • ASP.NET futures July 2007 (includes the XAML server control)
  • Silvelight 1.1 Tools for Visual Studio 2008 (makes the business interaction much more convenient using the "Add Silverlight link…" feature)

Additional tools:

  • Microsoft Expression Blend 2 Preview

Download source for SilverlightCalendar1.1 (including DB).

SilverlightCalendar2 [updated]

I have rewritten the calendar to support Silverlight 2 Beta 2

This version can be downloaded here.

Happy Silverlight coding :)


Don't steal images – a great story

Tuesday, October 30th, 2007 | General | 4 Comments

Well, this heading seems a little odd. But I just read on a personal, but great story on how to act if somewhone stole your picture. In this case a leading retailer stole a picture from a Erik Solheim and used it in a product catalogue. The picture showing his son with some hardware makes it even worse in my eyes. They had no rights on using the picture, and they just didn't understand that this is illegal. Read more on

Me – the independent consultant

Tuesday, October 9th, 2007 | .NET | No Comments

I recently quit my job in BEKK, and started as an independent consultant. If you need my services, especially in .NET and architecture, feel free to contact me.

Compressing ASPX, CSS and JS in ASP.NET 2.0

Thursday, January 18th, 2007 | .NET | No Comments

You always need to take some choices regarding performance on sites with a lot of traffic, and compression can be a solution. Of course, you need to be aware of the consequences by compressing. The CPU on the server and client will do some more work compressing and decompressing, but bandwidth will be significantly better.

By using the standard compression techniques like GZip and Deflate most modern browsers support compressed files. This was suggested in HTTP 1.0 and specified in HTTP 1.1. This also means that most crawlers and other services using standard protocols support compressed content.

So, what do you need ?

There are several ways to compress content and files. One is to let IIS compress it for you. I haven't tried this as people are reporting some hacking in the metabase in IIS to achieve this. Seems NOT the way to go, if you know what the metabase is.

The other approach is to write your own HttpModule and let this do the job. There are of course commercial products doing it for you, but it's not really a lot of code to create this module. You can start downloading Ivan Porto Carrero's excellent example. Compile it and add the http module to your web.config file. You should now see that the ASPX page is reduced to a minimum. Well, if you don't see it, download the Web Developer Firefox plug-in and you should get something like this (Information -> Document Size):

The module will compress anything handled by ASP.NET, which currently is not the case for CSS and JavaScripts. They are served directly from IIS. To compress these as well, you need to map CSS and JS files to .NET in IIS. You simply do this by configuring file extensions in IIS:

Notice! You should exclude WebResource.axd and ScriptResource.axd in the section for the HttpCompression handler in web.config. If you compress WebResource.axd, the standard .NET validating scripts and postback script will stop working. ScriptResource.axd is used by MS AJAX and has it's own compression module that will take care of the AJAX scripts. Typical the HttpCompression section should look like this:


<HttpCompress compressionType="GZip">


<add path="scriptresource.axd" />

<add path="webresource.axd" />



<add mime="image/jpeg" />

<add mime="image/jpg" />

<add mime="image/gif" />

<add mime="image/png" />




Now you should should have reduced the bandwidth to serve the page. As mentioned earlier, this might not be the best approach for all sites. You need to consider whether you have enough CPU on the server to compress everything or if you have enough bandwidth to serve files uncompressed. If you are short on both, then you probably should consider buying new hardware or upgrade your bandwidth . Also remember that the files are decompressed and cached in the users browser. This is not the case on the server. Every page requested will be compressed after the page is rendered.
› Continue reading


The near future of TFS

Friday, December 1st, 2006 | .NET | 2 Comments

Brian Harry who is the responsible manager for Team Foundation Server is blogging about upcomming features and fixes to TFS. It seems promising, but they should definetly release is soon, as a lot of people are waiting for the release. In particular the features of the Orcas version seems very welcome. I hope they will arrive sooner that soon.

Take a look at:

› Continue reading


sisli escort goztepe escort