Posts in the 'ASP.NET' category

May
30
2010

Umbraco: Breadcrumb Trail



Posted in ASP.NET and Programming

I’ve been using Umbraco recently, and always find it hard to find XSL templates or spend hours tweaking one when countless other people must have done the same thing, but not published the finished stylesheet.

Below is a navigation breadcrumb template which prints a trail to the current page, starting with level 1.
Read more »

Mar
8
2010

Entity Framework in Mednium Trust



Posted in ASP.NET

ASP.NET and medium trust typically don’t get along well once you start using third party libraries. It really should, however, be easier to get the Entity Framework working on medium trust, and this had me baffled for a few hours trying to split the EDMX file and other fruitless solutions.

To get this working I needed to:

  1. Move the EDMX to a separate assembly.
  2. Set the EDMX metadata artifact processing to ‘Embed in output assembly’.
  3. Add a reference in the site.
  4. Change the connection string to
    <add name="ShopEntities" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=servername;Initial Catalog=dbname;User Id=username;Password=P@55word;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

Which every guide claimed would work. There’s one more step if you have custom partial entity classes (in App_Code). If so, these also need to be moved to your output assembly, as you can’t split partial classes across assemblies. Lastly, change the namespace in your partial classes to the same as your output assembly, and everything should work.

Jul
25
2009

ASP.NET: Remove from the cache using



Posted in ASP.NET and Programming

The Cache object in ASP.NET isn’t like other collections, probably because it needs to be specialized so it doesn’t make use of generics. Unfortunately, that makes it more difficult to work with. This small utility method removes an object from the cache by key using a predictate. You could easily modify it to compare either of the IDictionaryEnumerator’s Key or Value properties.

/// <summary>
/// Removes an item from the cache using a Predictate to match the key.
/// </summary>
/// <param name="keyCriteria">The Predictate to use for each key to determine whether the entry should be removed.</param>
/// <summary>
/// Removes an item from the cache using a Predictate to match the key.
/// </summary>
/// <param name="keyCriteria">The Predictate to use for each key to determine whether the entry should be removed.</param>
public static void RemoveFromCache(Predicate<string> keyCriteria) {
	IDictionaryEnumerator enuma = HttpContext.Current.Cache.GetEnumerator();
	while(enuma.MoveNext()) {
		if(keyCriteria(enuma.Key.ToString())) {
			HttpContext.Current.Cache.Remove(enuma.Key.ToString());
		}
	}
}
Jul
9
2009

ASP.NET: Data Binding with ObjectDataSource



Posted in ASP.NET and Programming

In the past when writing forms with ASP.NET I’ve usually used an SqlDataSource when binding to controls, but I’ve been doing a lot of WPF lately. Being a desktop platform, WPF has excellent support for DataBinding, but obviously since HTTP is a stateless protocol (apart from the ViewState in ASP.NET which isn’t ideal), it’s not so well suited to data binding.

One thing I’ve never really tried is binding an object to a form to display a single item (eg: a report). This leads to writing lots of tedious code like:

	Order theOrder = GetOrder(orderId);
	litName.Text = theOrder.Name;
	litID.Text = theOrder.ID;
	litCustomer.Text = theOrder.BillingCustomer.Name;
	...

Read more »

Jul
5
2009

Faster Image Loading With jQuery



Posted in ASP.NET and Programming

Normally, if there are images on a webpage they’re there to support the content and not excessive, so you don’t have to worry about loading times. What if you need to have an undefined number though, and they happen to be high resolution like a gallery?

For the last site I worked on, the client wanted a gallery of 400×400 pixel images to cycle in a gallery on the front page of the site. They’re about 70KB each so if you’re loading 10 or more of them in the middle of the page, they’ll slow the page load down until they’ve all totally loaded.

The solution I used was to add the images to the DOM after the rest of the content loads.

Read more »

Nov
22
2008

GridView with Dynamic Columns



Posted in ASP.NET and Programming

The ASP.NET GridView control can be a really useful control for displaying and editing tabular data, but if you try to make it work with MySQL or a cross-tab query you’re likely to run into problems.

I wanted to create a grid showing the data from a cross-tab but also allow users to edit the data. If you’ve never used one before, a cross tabulation is a query which maps repeating rows to columns instead. This means that a column in a displayed result might not actually exist as a column in a database table. If you just need to display the data and aren’t bothered about editing it, the GridView works out of the box and will automatically generate the columns for you – but what if you need to display some columns but not others?

Read more »

Nov
9
2008

Cross page request variables in ASP.NET



Posted in ASP.NET and Programming

If you have a page which uses a master page, then put controls inside a ContentPlaceHolder on the sub page, accessing the values of the controls can be difficult if you’re posting the form to another page. The problem is, the ContentPlaceHolder mangles the control names so instead of radApplication, you get ctl00$cntMain$radApplication, meaning you can’t read them from Request.Form.

Hardcoding the name of the placeholder would be a bad idea incase you ever change the name of it or change the master page. There are actually two ways you can get at the controls.

Read more »