1

I have this control:

enter image description here

I'm trying to create a kind of validation, that whenever the user enters text to the TextBox, the "Add" button will be Enabled, and when the text is "" (null), the "Add" button is disabled. I dont want to use validators.

here's the code:

protected void addNewCategoryTB_TextChanged(object sender, EventArgs e)
    {
        if (addNewCategoryTB.Text != "")
            addNewCategoryBtn.Enabled = true;
        else
            addNewCategoryBtn.Enabled = false;
    }

The problam is, that when the user enter's text, the "Add" button doesn't changes from disabled to enabled (and vice versa)...

any ideas?

3 Answers 3

6

Is it Web Forms? In Web Forms the TextChanged event of the TextBox won't fire by default. In order to fire the event, you have to set the AutoPostBack property of the TextBox to true.

BUT, this would perform a HTTP post, what is kink of ugly, or you can wrap that in an UpdatePanel

A more elegant option, is to do that using jQuery, to do that in jQuery, you'll need some code like:

$(document).ready(function() {
    $("#<%= yourTextBox.ClientID %>").change(function() {
        var yourButton = $("#<%= yourButton.ClientID %>")

        yourButton.attr('disabled','disabled');
        yourButton.keyup(function() {
            if($(this).val() != '') {
                yourButton.removeAttr('disabled');
            }
        });

    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

$("#<%= aspControl.ClientID %>") asp.net runs server side and creates a clientID for that control, Jquery needs the clientID in order to access the correct control it wont do it simply by control Name.
thank you. actually, I dont want to use jQuery, so Ill think of different way to validate it...THANKS!
@omi Why wouldn't you want to use jQuery? There are very few reasons not to use it in today's day and age.
jQuery adds to, and simplifies most of the common Javascript functions and makes your code a lot easier to read and maintain, not to mention it has awesome documentation that you can learn from. I highly suggest you take the time to learn it, it will save you time in the end.
4

You'll need to accomplish this with Javascript, since ASP.NET is incapable of performing such client-side modifications. Think about it ... every time you pressed a letter inside the text box, it would have to postback and refresh the page in order to determine if the text box was empty or not. This is one way that ASP.NET differs from Winforms/WPF.

1 Comment

great answer! my background is actually winforms, so I needed this tip!
3

TextChanged events will make postback on server every time. You don't need to increase those request for such task.

You can use jquery to achieve this

var myButton = $("#btnSubmit");
var myInput=$("#name");
myButton.prop("disabled", "disabled");

myInput.change(function () {
  if(myInput.val().length > 0) {
    myButton.prop("disabled", "");
  } else {
    myButton.prop("disabled", "disabled");
  }
});

JS Fiddle Demo

You just need to take care of elements Id when you are using Server Controls. For that Either you can use ClientID or set property ClientIdMode="Static"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.