Changing Query String values in ASP.NET MVC

Recently I was trying to update a query string by adding or updating a parameter for URLs generated in a partial view.

I found an excellent example for achieving this by setting route data here however this didn’t preserve current query string values in the URL. I’ve extended upon that code and updated it to include the query string values in the route data which will be appended to the query string in the generated URL (assuming the parameter key doesn’t match part of the route).

Disabling an ASP.NET MVC input control

I love ASP.NET MVC. It is a great framework to get web apps quickly built. However using the built in HtmlHelper methods on your views isn’t without its issues.

An example of this is when trying to disable a HTML input control which has been built using HtmlHelper. You can add the “disabled” attribute which would be required by making use of one of the helper’s overloads however if you want to conditionally disable a control (based on your view model) it can result in your view’s code becoming more inflated than you might have initially though it would need to be!

To try and help with this I created a simple extension which will add the required attribute based on a passed in boolean value.

This is a very simplistic helper which can be improved upon but it served it’s need when it was created.

It can be used as follows.

Fixed/Sticky Headers on ASP.NET GridView using jQuery

The GridView control in ASP.NET Web Forms is an extremely useful and quick way of getting data out to the user in a nice tabular format. It does have one weak spot though and that is some of the tags it uses when doing this. Specifically, not putting the header information inside a <thead> tag.

So why is this important?

When you have a LOT of rows (as some do) after you’ve scrolled down the page and the table headers are out of view you can forget which column is which, especially if the data is similar (such as lots of numbers).

A quick and easy solution to this is to use a jQuery “sticky” or “fixed” table header plugin such as jquery.floatThead.

The problem comes in when you try and apply one of these plugins to a GridView. Since the GridView doesn’t use the <thead> tag the plugins often can’t find the header information which they are supposed to be fixing at the top of the screen after the user has scrolled down, as a result they just don’t work.

The Solution

There a two options to fixing this problem. The first is to use the DataBound event to change the header row to actually be a header row (why this isn’t the default I don’t know). The second option is to use a JavaScript/jQuery fix to make the first row in the table a table header.

In this case we’ll be using jQuery to fix the table to make it compatible with other jQuery plugins.

I stumbled upon a JavaScript fix by Rick Strahl which I wrapped up in a jQuery GridView Fix plugin so that it fit in with the rest of the project.

After either fix has been applied the table should now be compatible with other jQuery plugins.

Any tables also start to take on correct styling from CSS frameworks such as Bootstrap which also use the <thead> tag.

Strongly Typed Data in ASP .NET Web Forms

In ASP .NET Web Forms you’ve previously had to use strings to specify which properties you’d like to output when databinding, which would probably end up looking something like the following.

In all likelihood it could probably look far less tidy if you’ve ever needed to do any formatting on values you’re about to output to the page. Not to mention the errors you’ll get if you ever rename a property on the model and forget about your web page (because Visual Studio will not warn you about these errors).

In .NET 4.5.1 a strongly typed data feature was introduced for ASP .NET Web Forms. This allows you to specify a type for your data on the binding control which lets you be able to specify properties to bind without the use of strings.

You can make use of this feature by writing code similar to the following.

As shown above you can now specify a type for the data you are about to bind using the ItemType property of the databinding control. This lets you then use the Item property in the templates to specify which properties of your model you would like to output (as well as allowing you access to any handy extension methods you may have for formatting output).

This on whole means you know if a change you have made has broken your model binding before runtime, which is definitely a great step forward for ASP .NET Web Forms development.

Send E-mail through Microsoft Exchange using .NET

Recently I needed to send an e-mail using Microsoft Exchange Server from an ASP .NET Web App.

My first attempt involved using the classes in the System.Net.Mail namespace however I quickly realised this wasn’t the way to be going about it as I had very little luck getting a connection to the server.

After a while spent with Google I realised that there were some web services which could be used to perform tasks, these were called Exchange Web Services (EWS) and thankfully Microsoft provide a managed API which can be used to connect to and use them!

I thought this was exactly what I needed to I was surprised to be informed that the API was already installed when I tried to run the installer. I can only imagine that this was because I had Office already installed or because I installed Visual Studio with all the optional components.

In any case I eventually found the 32-bit version at the following path:

C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.0

Below is a simple example of how to use the EWS Managed API to connect to an exchange server and send an e-mail message.

I hope this saves someone some time when hunting for a quick solution!

Using a RequiredFieldValidator on a DropDownList in ASP .NET

In most applications today drop-down lists default to a “Please select..” option with some form of validation to ensure the user makes their own selection.

Drop-down list validation example

In ASP .NET web forms this frequently results in a drop-down list being created using code similar to that displayed below.

We can add an ASP .NET RequiredFieldValidator and set its ControlToValidate property to the ID of the DropDownList control as shown below.

This code would result in the drop-down list always passing validation since theRequiredFieldValidator sees the default value of “-1” as a valid value.

To correct this we need to set the InitialValue property of the RequiredFieldValidator to be the same as the initial value of the DropDownList (in this case “-1′) as shown below.

After this property has been set the DropDownList control will not pass validation until the user has selected a different option.

How to allow HTML content in ASP .NET MVC

Sometimes in web development, you need to provide an easy-to-use editor for non-technical users to input or change content where extra styling is required such as making text bold or italic.

However unless you use (or code yourself) an editor which uses some form of BBCode you’re going to come across the following error message if you’re using ASP .NET.

A potentially dangerous Request.Form value was detected from the client

 

I have seen various “fixes” to this suggested, the most frequent being turning off request validation entirely. While this will get your form working, it also lowers the security of your application since any text field will accept HTML content. This could be used to inject scripts or to deface your website, in either case this is bad news.

However in ASP .NET MVC you can specify the [AllowHtml] attribute above a property in your model to allow HTML content to be entered and maintain security in the rest of your application.

ASP .NET AJAX Control Toolkit Reorder List control and LINQ

The ASP .NET AJAX Toolkit is quite a useful and yet under-used set of controls. However they can also quickly lead to bizarre issues and worse performance than normal if they are not properly understood or used correctly. This issue is compounded when you take into consideration that quite a few of the controls included have a number of fairly unique bugs in them.

An instance of this is in the Reorder List control which is intended to allow a user to re-order a list of items by simple dragging and dropping them, instantly altering the database when they do it. While the control works, it appears to have some pretty strange behaviour in how it updates its list when a user drags and drops an item into a new position.

Put simply, it requires the update method signature to contain all the properties it is aware of on the list of objects you databind it to. This had me stumped for quite a while when I was investigating why this control was not working as expected.

Eg. You have an class called generated by LINQ to SQL Person and this class has the properties FirstName, LastName and Age. You then set a List of people as the Reorder list’s DataSource, everything works as expected. Great!

However unless your Update method’s signature contains your object’s Id, FirstName, LastName and Age the Reorder List will fail to commit the changes to the database and you will find your re-ordering mysteriously disappears when the page is reloaded.

To resolve this, simply change your method’s signature to include the missing object properties as relevant and everything will work as expected. The additional parameters do not need to be used in the method they just need to be there in order for the control to work as expected.

I hope this helps someone out since it kept me entertained for hours during the development of a project and didn’t seem to be too well documented online with regards to using LINQ to SQL with this control.