TypeInitializationException when using [IntegerValidator] with a [ConfigurationProperty]

by Dominic Zukiewicz 26. February 2010 15:14
I was writing my own custom configuration class to reduce the amount of <appSettings> in my code, but also to make sure the values were type safe. I wrote a simple test application to try this out. 1: class MyConfigurationTestConfiguration : System.Configuration.ConfigurationSection 2: { 3: private static MyConfigurationTestConfiguration settings = ConfigurationManager.GetSection("MyConfigurationSettingsConfiguration") as MyConfigurationTestConfiguration; 4:  5: public static MyConfigurationTestConfigurat... [More]

Tags: , ,

C#

FindControl() recursively using an Extension Method

by brad 26. January 2010 11:15
FindControl() is great if you’re looking for a control one level down, but if you’re making a dynamic control with n levels of child controls it can become a pain… Extension Methods to the rescue! Not only does it find the control your looking for at any level in the page, it also used generics to return the control without needing to cast it. /// <summary>/// Finds the control (Recursively) with an ID matching that given, of the Type requested. /// No need to cast either!/// </summary>/// <typeparam name="T">The type of control to return</typeparam>///... [More]

Tags:

ASP.NET | .NET 3.5 | C#

Updating EPiServer Version: Login Failed for User ''

by Brad 14. August 2008 14:27
I've just spent too long trying to upgrade EPiServer CMS 5 to SP2 but kept getting the following error: Login failed for user ''. The user is not associated with a trusted SQL Server connection. Turns out the problem was that EPiServerManager was pulling the connection string out of my web.config, but it appears it was ignoring the fact it specified an Integrated/Windows connection and was passing an empty SQL username to the database - hence the empty quote marks above. (cheers for your help Steve!) Solution: Temporarily change the SQL connection string in the site's web.config to... [More]

Tags:

C# | EPiServer

EPiServer: Dynamic SiteMap.xml generator for Google

by Brad 12. June 2008 10:09
Having stumbled across Google's Webmaster Tools a while ago it's been in the back of my mind to create a custom HttpHandler to dynamically create a (virtual) SiteMap.xml file that Google (and other search engines) can use as a reference when spidering my sites. I thought I'd share the basic implantation as a starting point. Ideally you'd add a property to each EPiServer PageType page for "change frequency" and "priority" which the sitemap generator would then use, but for this basic version I've simply set the homepage to 1.0 (the maximum priority) with a daily change, and all other pages to ... [More]

Tags:

ASP.NET | C# | EPiServer

EPiServer: Get a Page's Friendly URL

by Brad 9. June 2008 11:33
As part of my Search Engine friendly SiteMap.xml generator (using a HttpHandler - expect a blog post shortly...) I needed to output the friendly URL for each page in the site. However a call to PageData.LinkURL returns the actual link to the page (example shown below), not a "friendly" one that gets displayed in the browser address bar, or in any Hyperlink within the site. /MySite/Default.aspx?id=26&epslanguage=en-GB I had a look through the EPiServer documentation and couldn't find anything obvious so resorted to creating my own method to do it (shown below). Simply pass the rele... [More]

Tags:

ASP.NET | C# | EPiServer

C#: Google Map Server Control - Part 2 - Custom EPiServer Property

by Brad 9. June 2008 09:24
This post tells you how to take what I discussed in Part 1 and turn it into a custom EPiServer property, so CMS user's can simply enter a postcode and it will call of to Google's Local Search API, return the Coordinates for the postcode and plot a marker on the map. The user can then drag the marker for a more exact location, set the zoom level and finally the type of map (Normal, Satellite or Hybrid). The data is then rendered on the public site using the code from Part 1. The screenshot below show's how it will look in EPiServer (click the thumbnail for the full size version): Creating a... [More]

Tags:

ASP.NET | ASP.NET AJAX | C# | EPiServer

C#: Google Map Server Control - Part 1

by Brad 6. June 2008 15:17
Following on from my Google Analytics server control, I wanted make something "cooler", so creating a Server Control to render a Google Map control seemed ideal. Part 1 covers the creation of the this control, stay tuned for part 2 which will focus on creating a custom EPiServer property so it can be used in the EPiServer edit area, featuring Geocoding to lookup the coordinates from a UK post code, Map type selection and zoom level. Anyway here's the generic bit that can be used on any site... First off we need a set of basic properties to hold Latitude, Longitude, Zoom Level and Map Type: ... [More]

Tags:

ASP.NET | ASP.NET AJAX | C# | EPiServer

C#: Google Analytics Server Control

by Brad 4. June 2008 11:20
Today I've been consolidating UserControls that I've found myself using in one EPiServer project after another into a nice Server Control library. One such control that gets used on every site (even non-EPiServer ones) is one which adds the Google Analytics JavaScript code to the page. I decided this would be the first UserControl to migrate. Firstly I created a constant GoogleAnalyticsJavaScript which holds the JavaScript string to be added to the page. The only change I needed to make was to dynamically add the unique Google Analytics tracking code that was generated specifically for my dom... [More]

Tags:

ASP.NET | C# | EPiServer

C# Register onUnload JavaScript programmatically in code-behind

by Brad 3. June 2008 14:22
Having just created a Google Map server control I spent some time trying to register the relevant unload scripts that the Google API demands. However there was no easy way to get access to the body tag, and I certainly didn't want to have to edit the body tag, adding runat="server" as that would ruin my nicely "wrapped" control, I simply want a developer to drag it out of their Visual Studio Toolbox and it work... So after some thought I tried the following, which worked a treat: //Now we need to add the google maps //ONUNLOAD JavaScript function (it its not be added already) if (!Page... [More]

Tags:

C#

EPiServer Language Selector

by Brad 28. May 2008 12:55
Today I needed to create a UserControl to allow a user to select to view the current page from a list of enabled languages. To begin with I thought it would be a fairly easy task, by simply calling GetLanguageBranches() in the DataFactory class. However this ONLY returns the versions of the page that have been translated, which didn't suit me as our requirement was that ALL (enabled) languages should be displayed in the language selector, and if a language version doesn't exist for the specific page then the fallback language be used. So next I tried looping through the results from EPiServe... [More]

Tags:

ASP.NET | C# | EPiServer

C#: Getting round the &quot;Indexed Pixel Format Issue&quot; in GDI+

by Brad 21. May 2008 09:49
If you've ever tried to manipulate images in C# using GDI+ you'll almost certainly have come across the following error at some point: a graphics object cannot be created from an image that has an indexed pixel format I know I have, and it took a fair bit of effort to find the solution so I thought I'd share it. Here's a quick background on Pixel Formats... In 'Non Indexed' images, each pixel represents ONE colour. So a pixel might have a value &h0000FF (Red). In 'Indexed' images each pixel value is an index to a so-called 'palette' or 'colour table'. So a pixel might have a va... [More]

Tags:

C#

URL Rewriting: ASP.NET HyperLink ImageUrl Bug

by Brad 20. May 2008 08:12
I've been experimenting with writing my own C# URL Rewriter using a HttpModule. It's surprisingly simple as .NET kindly has the following (where newURL is the path to the actual file) : HttpContext.RewritePath(string newURL) So this allows a URL such as: /my-friendly-url to actually point to: /pages/content.aspx?id=5   Problem: All was going well, but some pages were giving the following error for no apparent reason: Cannot use a leading '..' to exit above the top directory. After some investigation I tracked the problem down to any asp.net Hyper... [More]

Tags:

ASP.NET | C#

How to access Session in custom HttpHandler

by Brad 19. May 2008 08:38
Creating a custom HttpHandler is fairly simple, all you need to do is implement the IHttpHandler interface. public class MyHttpHandler : IHttpHandler I recently needed to access the Session object from within my HttpHandler to check if a value existed, however the HttpContext.Session object was always null! After several minutes of pulling my hair out I discovered I simply needed to implement an additional 'marker' Interface. As I only needed Read Only access to the session object I used IReadOnlySessionState as follows: public class MyHttpHandler : IHttpHandler, ... [More]

Tags:

C#

Indent ListItems in DropDownList

by Brad 13. May 2008 14:30
If you've ever wanted to indent ListItems in an ASP.NET DropDownList you've probably come up against a brick wall if you use a simple space. It will indent in the actual HTML OK, but browsers will simply ignore the whitespace and render all the items inline. The trick is to do the following to enter the space, not use the spacebar: Hold down "ALT" while typing 0160

Tags:

C#

Creating C# Class from an XML file

by Brad 25. February 2008 09:16
If you've existing XML files that you need to manipulate in C# there is no need to write your own class by hand. Visual Studio ships with a little known tool XSD.exe which will do all the leg work for you. Step 1 (from a command prompt): This will create an XSD based on the XML file, which in step 2 will allow the tool to create the C# class(s) XSD.exe [XML_FILE] Step 2 (from a command prompt): Call the tool again, this time providing the newly created .xsd file, along with /c (for classes, you could use /d for a dataset) and /n for your project's namespace XSD.exe [XSD_F... [More]

Tags:

C# | Microsoft Visual Studio

ASP.NET: Accessing other User's Profiles

by Brad 4. February 2008 15:56
One of the many improvements in ASP.NET 2.0 was the Provider based architecture, and the most under used (certainly by me) is the User Profile Provider (click here for more info). You simply define the properties you want your users to have in the web.config (see below) <profile enabled="true" defaultProvider="MyProvider"> <providers> <add name="MyProvider" connectionStringName="MyConnection" applicationName="/" type="System.Web.Profile.SqlProfileProvider" /> </providers> <properties> <add name="F... [More]

Tags:

C#

ASP.NET: Embed Images/JavaScript in a dll

by Brad 31. January 2008 08:42
Its good practice to separate out code that can be re-used into its own class library which can easy be referenced by other projects. Until now any resources such as images/JavaScript files that I'd created I needed to remember to copy to each web site that used the dll, ensuring that I created the correct directory structure so the paths would be correct. Then it dawned on me that you must be able to embed these files into the dll itself. Here's the steps I used to embed an image file for a AjaxControlTookit Calendar Extender that I was using in a custom server control (the same principle can... [More]

Tags:

ASP.NET AJAX | C#

C#: Deleting a ReadOnly file

by Brad 24. January 2008 14:02
Unfortunately the following code will fail if the file in question is ReadOnly: File.Delete(path); So I'd always recommend using the following (especially when Visual Studio keeps annoying checking in files that it shouldn't concern itself with!!) //Check the file actually exists if (File.Exists(path)) { //If its readonly set it back to normal //Need to "AND" it as it can also be archive, hidden etc if ( (File.GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) File.SetAttributes(path, FileAttributes.Normal); //Delete the file... [More]

Tags:

C#

C#: Getting a user friendly file size as a string

by Brad 24. January 2008 13:52
If you want to display the size of a file to the user simply doing the following will result in the size of the file in bytes: new FileInfo(PathToMyFile).Length However that's fairly useless in today's world of bigger and bigger files so along comes this neat bit of code: public static string GetFileSizeAsString(long size) { double s = size; string[] format = new string[] { "{0} bytes", "{0} KB", "{0} MB", "{0} GB", "{0} TB", "{0} PB", "{0} EB", "{0} ZB","{0} YB" }; int i = 0; while (i < format.Length-1 && s >= 1024) { s = (100 *... [More]

Tags:

C#

AJAX: FileUpload control inside an UpdatePanel

by Brad 23. January 2008 21:53
UpdatePanels work fine with almost all controls, one control that it does 'break' however is FileUpload. If you try and upload a file it will seem to work - but if you check FileUpload.HasFile it will always return false. Rubbish if you've got an UpdatePanel in your MasterPage! However there is a solution, in fact it's not the UpdatePanel that breaks it, but the asynchronous PostBack instead. So a neat trick is to register the button you're using to cause the upload to use a normal PostBack. This can be done as follows: this.ScriptManager.RegisterPostBackControl(btnUpload); Then when ... [More]

Tags:

ASP.NET AJAX | C#

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar