Using KnockoutJS with SignalR in ASP.NET MVC

KnockoutJS is a MVVM implementation for JavaScript written by Steve Sanderson, in my opinion the author of the best ASP.NET MVC textbooks available. Simply put it lets you bind a JavaScript object model to your HTML UI using a Read more

A MongoDB Tutorial using C# and ASP.NET MVC

In this post I'm going to create a simple ASP.NET MVC website for a simple blog that uses MongoDB and the offical 10gen C# driver. MongoDB is no NOSQL database that stores information as Binary JSON (BSON) in documents. I Read more

Linq To SQL Tutorial

Check out some of my other Linq to SQL posts: EntityBase Inheritance Modifiers with SQLMetal Linq to SQL with WCF Services Linq to SQL Framework (Repository/Business wrapper) ObjectDataSource binding with server side paging and sorting Load Options Generic Framework using reflection This is a basic tutorial for Read more

Using the jTemplate jquery plugin with ajax and ASP.NET

Posted on by Joe in Ajax, ASP.NET, JavaScript, jQuery | 17 Comments

Download source

A guy in work introduced me to jTemplates, a template engine plugin for jQuery. It allows you to easily bind JavaScript objects to a defined template and also has some other nifty features.

I’ve found jTemplates to be particularly good when using ajax to display any information quickly and easily.

First I’ll explain how to create a simple template to display data, and then how to use the foreach and cycle features.

Read more

Using jQuery to make ajax calls to an ASMX web service using ASP.NET

Posted on by Joe in Ajax, ASP.NET, C#, JavaScript, jQuery | 17 Comments

Download source

Making ajax calls to an ASP.NET web service using jQuery is too easy.  In this post I’ll explain how to do it!

Read more

Using jQuery’s Each method to iterate through a JavaScript array

Posted on by Joe in JavaScript, jQuery | Leave a comment

The Each method in jQuery is pretty useful and can be used to iterate through the elements within a jQuery object.

I’ve been using it recently to iterate through JavaScript arrays.  It saves having to use a for loop and the code looks nicer.

Read more

JSON Serialization using the DataContractJsonSerializer and C#

Posted on by Joe in ASP.NET, C#, JavaScript | 4 Comments

Download source code

Previously I’d done JSON serialization using the JavaScriptSerializer which is part of AJAX Extensions 1.0, but this is now obsolete.

.NET 3.5 introduced the DataContractJsonSerializer class.  The class sits in the System.Runtime.Serialization.Json namespace which is curiously hidden away in the System.ServiceModel.Web assembly.

The DataContractJsonSerializer can serialize a class that contains the Serializable attribute or any class defined as a DataContract.

Read more

JavaScript inline if statement

Posted on by Joe in JavaScript | Leave a comment

The JavaScript inline if statement is identical to the one in C#:

var forename = (gender == 'male') ? 'Joe' : 'Emily';

Escape curly braces when using String.Format or StringBuilder.AppendFormat

Posted on by Joe in C#, JavaScript | 2 Comments

I was using the StringBuilder to create a JSON string which included an array and therefore curly braces, but I got an exception as curly braces are used to define the parameters for the format.

sb.AppendFormat("{\"Forename\":\"{0}\",\"Surname\":\"{1}\"}", person.Forename, person.Surname);

At first I tried adding a backslash which is used to escape the quotation mark but this didn’t work, so I tried the double character trick but adding two curly braces which seems to do the job.

sb.AppendFormat("{{\"Forename\":\"{0}\",\"Surname\":\"{1}\"}}", person.Forename, person.Surname);

Linq to SQL Tutorial – Linq to SQL Generic Framework using reflection

Posted on by Joe in ASP.NET, C#, Linq | 1 Comment

Download source code

In a previous post; Base Repository/Business Logic wrapper, I talked about a basic Linq to SQL Framework I created. I then extended it in my ObjectDataSource binding with paging and sorting post to show how to use it with the ObjectDataSource.

The problem was that although it worked quite nicely for getting all records and getting by ID, if I wanted to perform any specific filtering I had to create a derived repository class with a specific method for each query.

In this post I’ll explain how I have extended this framework using reflection to allow filtering on multiple columns without having to created separate repository objects.

Read more

Linq to SQL Tutorial – Using Load Options to preload data immediately without lazy loading

Posted on by Joe in ASP.NET, C#, Linq | 1 Comment

Download source

With Linq to SQL lazy loading is used by default. That means that if one object contains another object, the child object will only be loaded when first accessing it. Using Load Options it is possible to tell Linq to SQL to also load the child object at the same time as loading it’s parent.

Read more

ASP.NET FormView SetFocus

Posted on by Joe in ASP.NET, C# | 1 Comment

I have FormView and on changing to Edit mode I wanted to set focus to the first textbox. I thought I’d do this hooking into the ModeChanged event but this doesn’t work as the textbox hasn’t been rendered.

I then tried overriding the Render method and setting the focus after rendering; at this point the control was created but I get an exception to say focus can only be set in PreRender, but in PreRender the control won’t be available.

One way I found to get this to work was to set the focus in a handler for the DataBound event of the FormView.

Read more

Read only Automatic Properties in C#

Posted on by Joe in C# | 1 Comment

Automatic Properties were a nice addition to C# 3 and I use them all the time. A lot of people may already know this but somehow I only found out this week; you can create a read only automatic property simply by adding the ‘private’ keyword before ‘set’:


public class UserEventArgs : EventArgs
{
    public UserEventArgs(int userID)
    {
        UserID = userID;
    }

    public int UserID { get; private set; }
}