Button Enable disable when we write some text into TextBox in ASP.NET

March 28, 2015 , 0 Comments

I mean to say, we have three controls, two TextBoxes and one Button control. When we run the source code in the browser button is disabled by default. If we want to enable button at run time then we must to input some text in both TextBoxes. For this type of problem we should go for java script.

<%@ Page Language="C#" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
        function change(textb,buttont)
            var firstt = document.getElementById('<%= TextBox1.ClientID %>');
            var secondt = document.getElementById('<%= TextBox2.ClientID %>');
            if ((textb.value.length>=1 && firstt.value.length >= 1) && (textb.value.length>=1 && secondt.value.length >= 1))

                document.getElementById(buttont).disabled = false;
                document.getElementById(buttont).disabled = true;


    <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server" onkeyup="change(this,'Button1');"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"  onkeyup="change(this,'Button1');"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" Enabled="false" />


  1.  onkeyup() function call when we input character into text box.
  2.  change() function have two parameter first is TextBox instance and second is button reference.
  3. Access both TextBoxes by using document.getElementById( ) method.
  4.  Now, check the length of the string which is entered in the TextBox.
  5. If, Length is greater than equal to 1 then Button is enable otherwise disable.

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google