Jan 3

Written by: Soul Solutions
Wednesday, 3 January 2007 

BronwenWeeGo.jpgWanted a watermark on a text box similar to local.live.com and came across Deobrat's post which i found really useful.

I've converted the code to c# and used the textbox.ClientID instead of ID as we are using anthem etc ID doesn't work for us.

Here's the code:

using System;
using System;
using System.Text;
using System.Web.UI.WebControls;

namespace Helper
{
    public static class WatermarkHelper
    {
        public static void ApplyWaterMarkToTextBox(ref TextBox textBox, string waterMarkText, 
                                            string waterMarkStyle, string normalStyle)
        {
            textBox.Attributes.Add("OnFocus",
                                   "javascript:js_waterMark_Focus('" + textBox.ClientID + "', '" +
                                   waterMarkText.Replace("'", "\'") + "','" + waterMarkStyle + "', '" + normalStyle +
                                   "')");
            textBox.Attributes.Add("OnBlur",
                                   "javascript:js_waterMark_Blur('" + textBox.ClientID + "', '" +
                                   waterMarkText.Replace("'", "\'") + "','" + waterMarkStyle + "', '" + normalStyle +
                                   "')");
            textBox.Text = waterMarkText;
            textBox.CssClass = waterMarkStyle;
            if (!textBox.Page.ClientScript.IsClientScriptBlockRegistered("WaterMarkScript"))
            {
                StringBuilder scriptBuilder = new StringBuilder();

                scriptBuilder.Append(@"" + Environment.NewLine);
                textBox.Page.ClientScript.RegisterClientScriptBlock(textBox.Page.GetType(), "WaterMarkScript",
                                                                    scriptBuilder.ToString(), false);
            }
        }
    }
}

Tags:

1 comment(s) so far...

Re: Text Box Watermark

I never thought my article would actually render useful to anyone.. Good to see that you found it useful.
These days, I blog at http://blog.deobrat.com
You may want to have a look at it.

By Deobrat Singh on   Tuesday, 25 March 2008

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 
Copyright © 2002-2009 Soul Solutions Pty Ltd. | Login