ASP.NET - How to disabled/enabled button when textbox is empty.

ASP.NET - How to disabled/enabled button when textbox is empty.

Here is the example of how to disabled/enabled button when textbox is empty in asp.net. For that we will use javascript inside in asp.net to perform this like of functionality.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="disableEnableBtn.aspx.cs" Inherits="ASP.NET_GSH.disableEnableBtn" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Enable/Disable Button</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        
        <asp:TextBox ID="TextBox1" runat="server" onkeyup="disableBtn();"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server" onkeyup="disableBtn();"></asp:TextBox>
 
        <asp:Button ID="Button1" runat="server" Text="Button" Enabled="false" />
        
    </div>
        <script>
            function disableBtn() {
                var textbox1 = document.getElementById('<%=TextBox1.ClientID%>').value;
                var textbox2 = document.getElementById('<%=TextBox2.ClientID%>').value;
                var submitBtn = document.getElementById('<%=Button1.ClientID%>');

                if (textbox1.length > 0 && textbox2.length > 0) {
                    submitBtn.disabled = false;
                }
                else {
                    submitBtn.disabled = true;
                }
            }
        </script>
    </form>
</body>
</html>

Post a Comment

0 Comments