Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi i yogesh sharma. my problem is that i want to display the length of text as i type in a textbox, i have set the autopostback property of textbox to true, i write the following code on the textchanged event.
C#
protected void txtsubject_Textchanged(Object sender,EventArgs e)
{
textbox1.text=txtsubject.Text.Length.totring();
}

NOW iam getting the length of txtsubject.text in textbox1,but after the completion of the entire text in txtsubject(after pressing tab).
But I want the length of txtsubject.Text in textbox1 as i type correspondingly in txtsubject.
Please tel me what's the problem with my code. I have also set the autopostback to true. which i really does'nt want.
Please help me.. friends....Its urgent..I Have master page attached with my page having textbox. so please suggest me solution... where to use javascript function i:e in masterpage or in child page and in which tag...
Posted
Updated 18-Aug-11 6:18am
v2

You have to use Javascript for that.

Just Google this.

A good example is here.
 
Share this answer
 
v2
You can use javascript

JavaScript
<head>
    <script type="text/javascript">
        function Init () {
            var counter = document.getElementById ("counter");
            for (var i = 1; i < 1000; i++) {
                var option = new Option (i, i);
                counter.options.add (option);
            }
            counter.focus ();
        }

        function OnKeyPressCounter (event, counter) {
            var chCode = ('charCode' in event) ? event.charCode : event.keyCode;

            if (chCode == 43 /* + */) {
                if (counter.selectedIndex < counter.options.length - 1) {
                    counter.selectedIndex++;
                }
            }
            if (chCode == 45 /* - */) {
                if (counter.selectedIndex > 0) {
                    counter.selectedIndex--;
                }
            }
        }
    </script>
</head>
<body onload="Init ()">
    Use the + and - keys to increase/decrease the counter.
    <select id="counter" onkeypress="OnKeyPressCounter (event, this)" style="width:80px"></select>
</body>
 
Share this answer
 
Hi You Can use Javascript to achieve this.
Add following JavaScript function to yoyur page

JavaScript
function ShowTextLength()
        {
            var objLbl = document.getElementById("lblTextLength");
            var objTxt = document.getElementById("txtBox");
            if(objLbl && objLbl)
                objLbl.innerText = objTxt.value.length;
        }


Call this function on body Load and key up event of text box,

HTML
<body  önload="ShowTextLength();"></body>


Add Text Box and Labels on the page

ASP.NET
<asp:textbox id="txtBox" runat="server" onkeyup="ShowTextLength();">
        <br />
        <asp:label id="lblTextLength" runat="server">


Here I have used Label to display length of the text present in text box, you can take any control and modify last line of the javascript function as per control.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900