Life, the Universe and the Internet

My ramblings about making a living on the internet and life in general

How To Stop Losing The Focus With An ASP.Net AJAX Panel

I had a recent problem when working with ajax update panels on an asp.net application. I basically had a panel with lots of drop down boxes and text boxes inside it. Whenever a drop down was changed or a value was entered in a textbox, the whole panel would be recalculated as it contained totals and sub totals based on the contents of these boxes.

The problem I had was that I had to include all of the controls inside the update panel otherwise I would have ended up with 20-30 update panels on the page, a situation that I really wanted to avoid. Because the controls were inside the panel every partial postback I was losing the focus as effectively even though you don’t see it, the panel is destroyed and rebuilt every time.

So it became impossible for a user to just tab through the form updating values, as the focus was reset with every field, not an ideal user friendly situation.

So after much research I discovered that you can actually force focus to an individual control using the script manager control in the server side code. The code to do this is pretty simple, and would be something like:

Me.ScriptManager1.SetFocus(Textbox1)

Where scriptmanager1 is the name of your script manager control and textbox1 is the name of the control you want to set the focus too.

Now this is fine if you always know which control should have the focus, however if you have a lot of controls being processed inside one update panel you might not know which control to focus on next. Thankfully I found a neat little javascript on this blog http://couldbedone.blogspot.com/2007/08/restoring-lost-focus-in-update-panel.html That you can include with your script manager on any page, the javascipt will take care of all your focus problems for any update panels you have on your page. I’ll include the full script code below, don’t ask me to explain how it works, I’m just happy that it does and hope it will be of some use to you.

// JScript File

var lastFocusedControlId = "";

function focusHandler(e)

{

document.activeElement = e.originalTarget;

}

function appInit()

{

if (typeof(window.addEventListener) !== "undefined")

{

window.addEventListener("focus", focusHandler, true);

}

Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler);

Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler);

}

function pageLoadingHandler(sender, args)

{

lastFocusedControlId = typeof(document.activeElement) === "undefined"

? "" : document.activeElement.id;}function focusControl(targetControl)

{

if (Sys.Browser.agent === Sys.Browser.InternetExplorer)

{

var focusTarget = targetControl;

if (focusTarget && (typeof(focusTarget.contentEditable) !== "undefined"))

{

oldContentEditableSetting = focusTarget.contentEditable;

focusTarget.contentEditable = false;

}

else

{

focusTarget = null;

}

targetControl.focus();

if (focusTarget)

{

focusTarget.contentEditable = oldContentEditableSetting;

}

}

else

{

targetControl.focus();

}

}

function pageLoadedHandler(sender, args)

{

if (typeof(lastFocusedControlId) !== "undefined" && lastFocusedControlId != "")

{

var newFocused = $get(lastFocusedControlId);

if (newFocused)

{

focusControl(newFocused);

}

}

}

Sys.Application.add_init(appInit);

Being Rejected As An Affiliate

Bizarrely I was turned down today for an affiliate account with Dixons for use on my digital entertainment site. The reason I found it bizarre is that I already have am affiliate partnership with Dixons for exactly the same site. Now it might sound strange that I would want two but the one I have is for the price finder facility on the site and the profit is shared with another company. So I wanted one of my own for other promotions.

I always find it strange that a company would turn down a website for affiliation unless that website is clearly offensive, illegal or represent a bad image in some way. How will that company know if the site is going to make them any more money if they don’t even give the site a chance.

I think I will email the affiliate company and see if they can tell me why I was rejected, just for my own curiosity if nothing else.

And So Begins A Brand New Year

Well 2007 is gone and here we are in another year. Hope everyone who might read this had a good year last year, a nice Christmas and will have an even better 2008. If you made resolutions, good luck with them. I made a few, one of which was to actually post something here now and then.

This was a very different Christmas for me, mainly because my daughter is now three and she suddenly realises what Christmas is all about. It really was wonderful to see someone soak up all the magic of this time of year and I realise now that these are really going to be some precious years for Christmas and I intend to make the most of them for her and us.

Had a few nice presents myself the coolest of which was probably a mini RC helicopter that you can fly in the house. It’s a pretty simple little thing but it’s tremendous fun and with a little bit of practice it soon becomes easy to fly. The only slight problem is that my dog has decided it’s some kind of creature to chase and I dare not let it fly too low now when he’s around or it will be in his mouth before I could blink.

I’m currently hooked on Call Of Duty 4 for the XBOX 360 which has to be one of the best looking games I think I have ever seen on any console. I don’t tend to play it much though because I won’t play violent games in front of my daughter she takes way too much in.

I don’t know whether it’s just new year Euphoria but I am actually looking forward to the year ahead and the challenges to come.

Happy New Year Everyone.

Matching Dates On Fields With Different Times In A Transact SQL (SQL Server 2005) Where Clause

Just a quicky little Sql Server tip that I came across the other day.  If you are ever in a situation where you want to compare two dates in a where clause in a Transact SQL statement, but those dates actually have different times, for example:

Date1 =’01/09/2007 01:00:00′
Date2 = ‘01/09/2007 05:00:00′

You can’t just say “Where Date1 = Date2” because the times don’t match, but what you can do is in your where clause first reset both the times slots to midnight then do the where comparison, as per the example below:

SELECT SomeFields FROM SomeTable
WHERE CAST(FLOOR(CAST(Date1 AS FLOAT)) AS DATETIME) = CAST(FLOOR(CAST(Date2 AS FLOAT)) AS DATETIME)

Basically what this does is convert the date to a floating point, the floor command then basically rounds it down to the nearest integer, then it is converted back to a date time, the end result is that the dates now both look like this:

Date1 = “01/09/2007 00:00:00”
Date2 = “01/09/2007 00:00:00”

Hope this will be some use to someone.

Optoma Themescene HD80, My Newest Toy

Optoma Themescene HD80This is a bit off topic for this blog, but I just have to post something about my new toy. As anyone whose read this blog enough will know I’m a bit of a film nut and I have a large screen and projector set up at home to get the full cinema effect.

Recently I’ve upgraded my projector, I didn’t really have any intention of doing it but I got to hear about a new full 1080p projector that had just been released, was getting rave reviews and was incredibly low priced compared to other 1080p models. The projector in question is the new Optoma Themescence HD80 and is available for under £2000. For this price I got the projector, and eight foot screen and a free replacement bulb voucher.

And boy what a projector it is, I was totally blown away with it. I managed to arrange a demo at Sevenoaks Hi-Fi in Bristol. They were demo-ing it with a PS3 and some Blu-Ray disks and it is just incredibly detailed, really bright, rich deep colours and amazingly quiet too. I’m running it with a Xbox 360 and HD-DVD Drive and the Xbox is very cool at this size and detail level.

In terms of value for money, I really think this projector is unbeatable, when you think that much of the competition at this level is up over £6000 or more there really is no beating it. If you’re interested in getting one, I really recommend seeking out a demo, you will love it.

New Blog Template And Some Behind The Scenes Changes

I’ve made some significant blog changes, some of which are obviously apparent and some which are more behind the scenes changes but very important for anyone who might comment or link here. So here is a list of the changes I’ve implemented:

  1. Template change: This one is of course blatantly obvious to anyone who’s been here before but I have changed to a completely new template. I hope you agree with me that this one is better, apart from looking better the programming code excerpts are now much easier to read and/or copy out.
  2. Dofollow plugin: It was only recently that I found out that by default wordpress does a “nofollow” on comments. So anyone who linked to their site in the comment would not have got the pr for it. So I’ve enabled this plugin and now all links on this site are set to pass pr onto the linking sites.
  3. Top Commentors Plugin: I’ve added a plugin that not only shows the top ten commenter’s on every page of the blog, but also the last five comments that are posted. If your interested in the plug, the authors site is here. Remember guys those links now also pass on pr.

I hope you like the new changes as much as I do, feel free to comment away.