Fill PDF Form Fields from an ASP.NET page using iTextSharp

2

Posted by Joe | Posted in ASP.NET, C# | Posted on 25-08-2009

Download the source code: PDFTest.zip

For a project I’m working on I needed to be able to generate PDF’s containing data from my page.  I came across a library called iTextSharp which is a C# port of the Java library iText.  Using this library allows you to generate PDF files on the fly.  You can download the library here, and need to add it as a reference in your project.

In Adobe Acrobat I created a rather crude looking PDF with two Form Fields for Forename and Surname, named txtForename and txtSurname respectively.

Read the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Using the CustomValidator’s ClientValidationFunction

0

Posted by Joe | Posted in ASP.NET, C#, JavaScript | Posted on 12-08-2009

The ASP.NET CustomValidator allows you to create custom validation that fits into its validation framework.  The CustomValidator has a server side event called OnServerValidate and a client side event called ClientValidationFunction.

You can use the CustomValidator by just implementing the OnServerValidate event, but any other validation controls that are validated client side will need to be valid before the CustomValidator will be used.  Doing this causes a postback to validate the CustomValidator and as it’s not client side the validation message will not be shown in a ValidationSummary.

Read the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Show a JavaScript confirm dialog box after client validation

3

Posted by Joe | Posted in ASP.NET, C#, JavaScript | Posted on 12-08-2009

It’s quite common to use a JavaScript confirm dialog on a button press to ensure that the user wants to submit the page.  If you are using any of the ASP.NET validators that use client side validation you will find the confirm appears before the validation, whereas it would be better if the confirm box only appeared if the page was valid.

Luckily the framework supplies some JavaScript functions that can be used for validation.  Page_IsValid is a boolean indicating if the page is valid which can be used in client script.  This value is set when the JavaScript function Page_ClientValidate is executed.

For this example I have set CausesValidation to false on the button that will show the confirm dialog, as I will call the client side function myself.  If I don’t to this my validation will be called twice.  In the OnClientClick I then need to add some JavaScript to call Page_ClientValidate if the function exists, and then only show the confirm dialog if Page_IsValid is true:

<asp:LinkButton ID="btnSubmit"
                CausesValidation="false"
                OnClientClick="if (typeof(Page_ClientValidate) == 'function') { Page_ClientValidate(); if(Page_IsValid) { return confirm('You are sure?'); } };"
                runat="server"
                onclick="btnSubmit_Click">

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Generate a CSV from a generic list of objects using reflection and extension methods

6

Posted by Joe | Posted in ASP.NET, C# | Posted on 03-08-2009

Download the source code for this post: ReflectionCSV.zip

Last week I wrote a post about generating a CSV from a DataTable. As I hardly use DataTables these days I thought it would be good to write a function that does this for a generic list of objects using reflection to get the properties of the object and export them.

In this example I have done this using an extension method on the generic list. This approach could also be easily applied to the DataTable example.

Read the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit