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

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">
    <title></title>
    <script>
        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;
            else
                document.getElementById(buttont).disabled = true;

        }



    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <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" />
    </div>
    </form>
</body>
</html>

Here,

  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.

Comments

Popular posts from this blog

difference between structure and union in C Language

Difference between Linear search and Binary Search in c language

Difference between static and dynamic websites