Archive for June, 2010

Show character count on typing in textbox ASP.NET

During my recent project and some before that I was in need to show the number of characters entered in a textbox to user so that she may know that the message (SMS or something) is touching the boundaries. Here in this post I will be discussing about showing a character count with max limit of 160, the counter shows in green until 140 is reached, turns orange when 140 exceeds and red when 160. You may find many examples over the internet about character count but I will also discuss the scenario when you edit the record and specially when you have 2 textboxes doing the same thing ie showing character count. Lets start with the javascript code called on: onKeyUp, onKeyDown and onChange events. In demo attached this function is present in script.js file

function textCounter(field, cntfield, maxlimit)
{
field = document.getElementById(field);

cntfield = document.getElementById(cntfield); // imp otherwise throws error in IE on focusOut from textbox
//cntfield.width = “330px”;
cntfield.value = maxlimit + field.value.length;

if (cntfield.value <= 140)
{
cntfield.className = “greenClass”;
cntfield.value = cntfield.value + ” characters are entered”;
}
else if (cntfield.value >= 140 && cntfield.value < 160)
{
cntfield.className = “yellowClass”;
cntfield.value = cntfield.value + ” characters are entered, You are nearing 160 characters limit”
}
else if (cntfield.value == 160)
{
cntfield.className = “redClass”;
cntfield.value = cntfield.value + ” characters limit is reached”;
}
}

In function textcounter 3 CSS classes are used that are present in style.css file in demo project attached. The asp.net textbox code is like that with the counter input showing the character count:

<asp:TextBox ID=”TextBox1″ runat=”server” CssClass=”txtbox”
MaxLength=”160″
onKeyDown=”textCounter(this.id,’remLen1′,0);”
onKeyUp=”textCounter(this.id,’remLen1′,0);”
onChange=”textCounter(this.id,’remLen1′,0);”></asp:TextBox>

&nbsp;<input readonly=”readonly” type=”text” id=”remLen1″
style=”border-style:none; border-color:inherit; border-width:medium; width:380px;”
name=”remLen1″ value=”0 characters are entered” />

The given code will work fine when inserting or just a single functionality on edit time though we need to set the textbox value and also update the character count like this:

TextBox1.Text = “edit text”;
ClientScript.RegisterStartupScript(typeof(Page), “txt1″, string.Format(“textCounter(‘{0}’,'remLen1′,0);“, TextBox1.ClientID), true);

The orange text calls the javascript function described earlier and the ; at the end is the key to call the same line for TextBox2. If not present the call will not occur because the script is not terminated if “;” is not given.

In the attached demo there is an Edit button to demonstrate the Edit scenario, please Download and let me know if you find any difficulties or error in implementing this.

Enhanced by Zemanta

I have developed some projects using WCF and on deployments I face an error thrown by IIS7 saying “The page you are requesting cannot be served because of the extension configuration” with an HTTP Error 404.3 – Not Found. It happens when I hit a URL something like: localhost/coreservice/myservice.svc

IIS7 does not recognize this extension and that needs to be installed from a utility called “ServiceModelReg.exe” found under C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation
This utility has many command line switches, the one required to vanish that error “-i” to install this version of WCF and update scriptmaps at IIS metabase root and below that.

How to: Its really simple navigate to the directory ie C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation from command prompt and type: ServiceModelReg -i and press enter. This will install the WCF version and you can now hit the service url without any error.

Tip: If you are disallowed by windows due to lack of privileges to execute ServiceReg, run the command prompt as administrator. You can do so by typing cmd in Win7 search menu under programs and chose run as Administrator

With this post listed here many others and I have the reference how to solve it in future.

Enhanced by Zemanta