0

I have the following textbox in my ASP.net page:

<asp:TextBox OnTextChanged="maxchar" ID="txtName" Width="95%" runat="server"></asp:TextBox>

The following in my code-behind:

protected void maxchar(object sender, EventArgs e)
{
    if (txtName.Text.Length > 25)
    {
        lblIsValid.Text = "only 25 characters allowed";
    }
}

When I enter text in the textbox and exceed the 25 character nothing is displayed in the label. How can I fix it?

Si8
  • 9,141
  • 22
  • 109
  • 221

4 Answers4

3

In this case, for the max lenght, you should use the property of asp:textbox called MaxLength="25".

But, if you want to send a message to user, i recomend to use JQuery, it's easy:

$('#txtName').keyDown(function(){
     if($(this).length >= 25){
        //you alert
     }; 
});
twcavalli
  • 58
  • 3
1

OnTextChanged only occurs during a post back MSDN reference.

You would need to add javascript to do what you want to do, since javascript runs locally it doesn't require a postback.

If you don't want javascript you will have to wait for the user to submit the page, it is an unavoidable restriction of javascript-free webpages.

Guvante
  • 18,775
  • 1
  • 33
  • 64
  • I am trying to avoid as the user might have the JS disabled :/ – Si8 May 29 '14 at 17:14
  • @SiKni8: If you don't want to wait for a postback and don't want to use JS there is no way to do this. You could try having the text box post back every character but that wouldn't work well. – Guvante May 29 '14 at 17:15
  • Unless you want to postback every single time (which is ridiculous), you have to use JS. – Ben Black May 29 '14 at 17:16
  • @BenBlack: Waiting for submit isn't too bad, it is what I would expect if I disabled JS in my browser. – Guvante May 29 '14 at 17:17
  • 2
    @Guvante - That's not necessarily true. If he's using HTML5, he can add a "required" attribute to the textbox and the "maxlength" attribute. See a [demo here](http://www.wufoo.com/html5/attributes/03-maxlength.html) – Icemanind May 29 '14 at 17:18
  • How do I code the `waiting for the user to submit` and use C# to sanitize the code? – Si8 May 29 '14 at 17:18
  • @SiKni8: Add an instance variable like `IsValid` and set it to `false` if you assign the text. Make sure to check that variable before you do anything with the submission. – Guvante May 29 '14 at 17:21
1

Pretty sure that should be TextChanged and that OnTextChanged should be a JavaScript call (I believe). Either way, you should do this in JS. Calling to the server on every text changed event should be avoided.

In jQuery you can do

$('#txtTime').changed(function(){
    // do event logic here
});

Now hopefully you're using jQuery. If not I can update for vanilla JS.

Tim Meers
  • 928
  • 1
  • 14
  • 24
  • JQuery will do except what if JS is disabled for the browser... – Si8 May 29 '14 at 17:15
  • 1
    Then wait for the user to submit the page and do your server side checks. Never said you should drop doing server side validation, just maybe don't force the user to postback on every change. *Really* bad for UX. – Tim Meers May 29 '14 at 17:30
  • I of course agree with you. I will add the JQuery for front end and incase it's disabled I will also do a server check. I tried using HtmlEncode which keeps giving my application an error. – Si8 May 29 '14 at 17:32
0

Have you tried setting autopostback="true" in the asp:TextBox declaration? A quick search turned up this article as well, maybe it will be some help.

Scott
  • 729
  • 1
  • 11
  • 30