Hint text in ASP.NET TextBox controls

June 3rd, 2008

More and more often these days, you see hint text in a textbox control on a web form.  You know the kind of thing - “user name” or “password” appearing in silver text within an input control.  Well, I had to implement that functionality today and it is surprisingly easy to achieve.

The first thing you need to do is create your TextBox control on the page.  Once you’ve done that, add the following method to your codebehind file.

private void SetTextBoxHints(TextBox textBox, string defaultText)
{
   textBox.Attributes.Add("onfocus", "clearText(this,'" + defaultText + "')");
   textBox.Attributes.Add("onblur", "resetText(this,'" + defaultText + "')");
   if (textBox.Text == "")
   {
      textBox.Text = defaultText;
      textBox.ForeColor = System.Drawing.Color.Silver;
   }
   else if (textBox.Text == defaultText)
   {
      textBox.ForeColor = System.Drawing.Color.Silver;
   }
   else
   {
      textBox.ForeColor = System.Drawing.Color.Black;
 }

 Next, you’ll need to add the javascript to the page/user control to look after the dynamic changes in content/colour. 

<script type="text/javascript" language="javascript">
<!--
function clearText(ctrl,defaultText) {
if(ctrl.value == defaultText)
  ctrl.value = ""
  ctrl.style.color = "#000000";
}
function resetText(ctrl,defaultText) {
if(ctrl.value == "")
{
   ctrl.value = defaultText
   ctrl.style.color = "#C0C0C0";
}
}
// -->
</script>

Then, you need to call the setup method for each Textbox you want to control.

SetTextBoxHints(myControl, "the text you want to show as default...");

And that, as they say, is that. Enjoy.

Multiple Async calls to a web service

May 21st, 2008

I’m currently working on a project that requires making several concurrent asynchronous calls to a web service.  Now, the service work fine synchrounously, but change the pattern slightly and I was getting the following error.

“There was an error during asynchronous processing. Unique state object is required for multiple asynchronous simultaneous operations to be outstanding.”

The solution, courtesy of a post on the MSDN forums was to pass a new GUID as a state object.

Problem solved.

 

Got to love the tech

December 4th, 2007

Who’d have thought, just a few short years ago, that I could be sitting on a train, using my MP3 player to write a blog post?  Got to love the tech. :)

Wild Garden

July 26th, 2007

I’ve been a bit slack with my back garden, but I have to say, I’m quite pleased that I left a couple of small patches to their own devices. They’ve sprouted a small number of wild flowers, and last weekend, they were just about as good as I’ve seen them.

The Amazing Google Strike Again!

July 3rd, 2007

This morning I was given a tracking number for a parcel I am expecting.  However, I didn’t get the name of the company who were to deliver it.  Not a good start if I wanted to use the tracking number to actually track the parcel.

Then I remembered a discussion at work about Google’s recognition routines, and one of my few remaining synapses fired into life.

I went to Google’s homepage, typed in my tracking number (nothing else, just the number) and hit search.  I have to admit I was very pleasantly surprised when the top of the results page said “Track UPS package 1Z6 …..  70″

Very neat.

Picasaweb - new slideshow feature

July 2nd, 2007

I’ve just seen that picasaweb have added a slideshow feature to their web photo albums.  I was going to post about something else, but I thought this was cool enough to put the other post to one side for a while.

 Here’s an example…

It had to happen sometime

June 23rd, 2007

It is almost inevitable in the modern world that someone will manage to get hold of at least part of your identity.  It happened to me this week.

I went downstairs first thing this morning to see what snail-mail had been delivered, only to find an urgent letter from my bank.  It seems that someone has managed to get hold of my bank card details and used them to spend around 4-500 pounds of my money.  It would have been more, but something tipped the bank off that there was something wrong and the put a stop on my card.

I am happy that the bank managed to catch this behaviour, and prevented another 500 pound being spent.  I can’t help but wonder, though, whether we are all a little complacent about our details.  I’ve always taken time to destroy paperwork that could hold vital information for use by an identity thief, but it seems that is not enough.  Just using your debit or credit card in the wrong place is enough to leave you open to fraud - a quick skim of your card details is all it takes…

It’s easy to be wise after the event, but now may be a good time to fall back on using good old cash for a while.

A fun waste of time

June 20th, 2007

Last night Chester was directly under a fair sized thunder storm with some really impressed bolts of lightning.  Seeing that I had my camera with me, and that it could be set for up to 3 second exposures, I decided to try to photograph some of the lightning.  Please note the use of the word “try”.  As you can imagine, it was something less than completely successful, but I am now the proud owner of about 60  almost identical digital images of the street outside my hotel with a totally calm and lightning-free sky above.

Next time…

High Royds Psychiatric Hospital

June 19th, 2007

Tucked away, back from the road between Guiseley and Menston is the High Royds Psychiatric Hospital.  Or rather, the buildings that house it. 

Build by the Victorians, and originally (and gloriously) named The West Riding Pauper Lunatic Asylum, the hospital closed its doors in 2003.  Now the property developers have moved in and are making the place into luxury housing.  Fortunately for those of us with a love of architecture, the buildings are grade II listed and as a result, the developers are not allowed to alter the external structure.

As you can see from the picture below, the buildings were connected by a series of covered walkways.  Unfortunately, these are almost all gone now.  They weren’t listed in their own right, so they have been demolished to make way for car parking.

You can see some really good photos of the place over at one of the urban exploration websites, but I’d really suggest that you get over there for a look yourself before the development is finished.  If you like Victorian utilitarian architecture, you’ll love High Royds.

ZombieSheep.co.uk

June 19th, 2007

Having just moved all my hosting, I am pleased to be able to say that the zombiesheep.co.uk site is back up.  It is nowhere near complete, and all the music and software is still awaiting upload, but the site is up, at least.

If you feel the urge, why not pop over for a look?

Technorati Tags: , ,