Hi Greg,
Good catch. The backspace key does not trigger the onKeyPress event.
Therefore, your else statement does not get called.
Here's a new sample that uses the onKeyUp event.
**** HTML
Add a text box and a button to the form.
Add this in the header section in HTML view.
<script language="javascript">
<!--
function FourChars() {
if (document.all("TextBox1").value.length > 3)
document.all("Button1").disabled = false;
else
document.all("Button1").disabled = true;
}
//-->
</script>
**** Code-behind
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Button1.Enabled = False
TextBox1.Attributes.Add("onKeyUp", "FourChars();")
End Sub
I hope this helps.
Thank you, Mike
Microsoft, ASP.NET Support Professional
Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer's security.
This posting is provided "AS IS", with no warranties, and confers no rights.<
[email protected]>
Subject: Re: Button Behavior
Date: Tue, 23 Dec 2003 14:36:51 -0600
Lines: 119
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#
[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
NNTP-Posting-Host: pc110-115.ahc.umn.edu 160.94.110.115
Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.
phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet.webcontrols:17041
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
Yes that basically does it, provided someone doesn't backspace some of the
characters out after the button is enabled. I tried adding an else
if(document.all("TextBox1").value.length >2)
document.all("Button1").disabled=false;
else
document.all("Button1").disabled=true;
statement to catch this but it doesn't seem to do what I want it to do. The
button only disables if you backspace all the characters out.
I'm sure I am missing something very obvious.
"Mike Moore [MSFT]" said:
Hi Greg,
Use the text box's onKeyPress event as follows:
*** HTML
<HTML>
<HEAD>
<title>z4</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="
http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
<!--
function FourChars() {
if (document.all("TextBox1").value.length > 2)
document.all("Button1").disabled = false;
}
//-->
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server"
Text="Button"> said:
</form>
</body>
</HTML>
*** Code
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Button1.Enabled = False
TextBox1.Attributes.Add("onKeyPress", "FourChars();")
End Sub
Does this answer your question?
Thank you, Mike
Microsoft, ASP.NET Support Professional
Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer's security.
This posting is provided "AS IS", with no warranties, and confers no rights.
--------------------
From: "Greg Smith" <
[email protected]>
References: <OA9#
[email protected]>
<
[email protected]>
Subject: Re: Button Behavior
Date: Fri, 19 Dec 2003 08:29:19 -0600
Lines: 22
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <
[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
NNTP-Posting-Host: pc110-115.ahc.umn.edu 160.94.110.115
Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08. I