• Client login
  • Work for us

All the latest from the team...

Tuesday, 2 December 2008

ASP.net C# ordering files returned by GetFiles

A very quick post today to share a bit of a moan about ASP.net and the Directory.GetFiles Method which most of you will no doubt know only returns a list of files order alphabetically. Now whilst we've been developing a system for a client we've had the need to order by date last modified - which I'm sure is a common request for many of you reading this. Well with no further ado here is a very simple class so you can sort the array returned by the GetFiles method...

public class FilesDescendingComparer : IComparer
{
#region IComparer Members

public int Compare(object x, object y)
{
return DateTime.Compare(((FileInfo)y).CreationTime,((FileInfo)x).CreationTime);
}

#endregion
}

To invoke this method is very simple too...

Array.Sort(theFiles, new FilesDescendingComparer());

Where theFiles is the result of your GetFiles call.

Happy coding!

Labels: , , , , , , ,

Wednesday, 19 March 2008

ASP.net 2 and the infamous Response.Redirect method

OK any of you working with a CMS system based on ASP.net which utilises the Response.Redirect method to handle links will undoubtedly have come across this issue - those of you using ASP.net in general and considering using Response.Redirect should also read on...

Response.Redirect makes a round trip to the server and returns a 302 redirect in the headers. Now if you're in anyway concered about SEO you'll know that the majority of search engine bots won't follow 302 redirects (temporary redirects).

I actually came across this after monitoring Google on a DotNetNuke project which utitlises the LinkClick method (which pretty much handles all links in the TEXT/HTML module). So I know that any links to LinkClick.aspx?xxxx&yyyyy will return 302 redirects before going onto my real "seo-freindly" url... Which of course is a BIG problem.

Well after badgering around for what seems like weeks (but is probaly only a few minutes) I began to think about the global.asax file and how it handles requests.

I know there is the Application_PreSendRequestHeaders (which is processed just before we bat the response headers back to the client) so this seems like an appropriate place to check and rewrite the headers and this is actually quite a simple piece of code...

Sub Application_PreSendRequestHeaders(ByVal sender As Object, ByVal e As EventArgs)
If Response.StatusCode = 302 Then
Response.StatusCode = 301
End If
End Sub

Obviously you'll want to be specific in your catchment but you get the idea. I tested this with LiveHTTPHeaders and it seems to have the desired affect - a 301 redirect. I'll let you know if Google likes it after Mr. Google-bot has come back to see my client's site. IN the meantime if your desired affect is to redirect to another file on your server I'd suggest you use the Server.transfer() method which doesn't make the round trip to the server.

Also check out Matt Cutts blog article about this very issue

Labels: , , , , , , , ,

Thursday, 13 March 2008

DNN setting the date format for the calendar control

Having recently been frustrated by the lack of support in DNN to change the format of dates (yes I know about the web.config setting and yes I know about the built in language settings - it still didn't work when using DataSprings DynamicForms module which otherwise is simply superb).

Anyway if you're experiencing the same issues that we did then there is a simple solution.

In the "js" folder you'll find a file named "PopupCalendar.js".

I won't bore you with the various ways in which you customise the look and feel but if you need to change the date format (in my case to the United Kingdom dd/mm/yyyy) here's how...

Simply scroll down until you find the lines:

//default localized short date format if not provided
if (popCalDstFmt == "")
popCalDstFmt = "m/d/yyyy";

This is where the date format is set if you didn't pre-specify a format on the call to the popupCal method.

You can simply override this by adding the following line straight after this function... In our case:

//force UK date format
popCalDstFmt = "d/M/yyyy";

...and voila you're ready to rock and roll!

Hope this helps some other frustrated developers!

Cheers
Jon

Labels: , , , , ,

Thursday, 6 March 2008

Adding a logout link to the control panel in DNN

DotNetNuke is a great tool but because it's written by developers sometimes basic GUI options have been omitted. One of these is adding a logout button the control panel - especially if you don't want a login/out link on your main webpages!

It's actually really simple to do this just navigate to the "admin/ControlPanel" directory of your site.

There are two different control panels: "iconbar" and "classic" just open up the Web Control that represents the one you are using: classic.ascx or iconbar.ascx.

Next you'll need to register the DNN:Login tag and associate it with the "Login" Control. To do this add the following line under the control declaration so your file will look like this:

<%@ Control language="vb" AutoEventWireup="false" Explicit="True" Inherits="DotNetNuke.UI.ControlPanels.IconBar" CodeFile="IconBar.ascx.vb" %>
<%@ Register TagPrefix="dnn" TagName="LOGIN" Src="~/Admin/Skins/Login.ascx" %>

Then to add the link itself just add the line:
<dnn:login runat="server" id="dnnLOGIN">

...Wherever you would like the link displayed... Simple!

Here's an example file for you to download: iconbar.zip

Labels: , , , ,

Thursday, 14 February 2008

ASP.net ButtonClick events not fired when "Enter" is hit?

In Internet Ignorer (AKA Internet Explorer for you purists)... You know that browser by MICROSOFT? The same dudes who came up with ASP.net? You may have noticed that a form submittal event that is fired by the enter key doesn't call the form's _Click event.

This only happens with a single TextBox control for example the code below:


<form runat="server">
Name: <asp:TextBox runat="server" id="txtField" />
<asp:Button runat="server" Text="Go" />
</form>


Internet Ignorer does not pass the submit button's name/value pair in the post. Therefore the ASP.net engine can't determine what control fired the postback event and therefore can't fire the appropriate Click event.

To get round this simply add another TextBox control to your form for example:


<form runat="server">
Name: <asp:TextBox runat="server" id="txtField" />
<asp:Button runat="server" Text="Go" />
</form>
>asp:TextBox runat="server" style="display:none;" /<


For some strange reason INternet Ignorer now decides to send the name value pairs along to the post event.

Yet ANOTHER workaround for a Microsoft product... When using a Microsoft technology... Does it get worse than this? :-)

Labels: , , , , , , , ,

Tuesday, 15 January 2008

ASP and 30x redirects for SEO

One of the common things I come across when building websites is the lack of understanding for SEO when you are porting to a new site.

One recent site I've just finished meant porting a site from classic ASP 3.0 to ASP.net and how to point redirects to the new pages to maintain the SEO rankings. Well the obvious answer is a 301 redirect. But how?

Well there are many ways on an IIS box but the basic way to point ASP (classic) pages is to have a global.asa AS WELL AS a global.asax. The global.asa will serve ASP (classic) and the global.asax will serve the ASP.net pages... Therefore we can simply put our redirect code into the global.asa and point our .asp files to their new .aspx conterparts...

If Request.ServerVariables("PATH_INFO")= "/default.asp" Then
If Request.ServerVariables("QUERY_STRING")="" Then
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "http://www.mydomain.com/"
Else
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "http://www.mydomain.com/?" &
Request.ServerVariables("QUERY_STRING")
End If
End if


I've added the check for querystring variables in case you wanted to pass any tracking or like URL variables to your new page but you can customise to suit your needs.

Labels: , , , , ,

RSS feed ATOM feed Add to Technorati Favorites View Jon Harvey's profile on LinkedIn