Button Behavior

G

Greg Smith

I am working on my first web app and I am confused by the default behavior
of Button control. When I try to use it to execute some code it takes one
click to get focus and a second click to actually execute the event. I
don't see my users liking this.

Any help is greatly appreciated.
 
M

Mike Moore [MSFT]

Hi Greg,

I was not able to reproduce this behavior on my machine. When I single
click a button it receives focus and runs the click event.

What browser are you using (including version)?

Here's an experiment you can do which will help me troubleshoot this
problem.

Start with a new webform and add a webform button control & a textbox to
it. Then view it in the browser, set focus on the text box, and then click
the button. Does it have the same problem of requiring two clicks?

If it has the problem, then select View Source in the browser and post the
text here.

If it works correctly, then we need to look at your page that has the
problem.

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.


--------------------
 
G

Greg Smith

I was not able to reproduce this behavior on my machine. When I single
click a button it receives focus and runs the click event.

What browser are you using (including version)?

It's 6.02800, but that is not the problem. It is two-fold.

1. - I am new to the web side and expected the TextBox TextChanged event to
behave as it does on the Windows side.
2. - I am dumb as a stump.

The problem existed because I had code in the TextChanged event
(AutoPostBack set to true) and when I clicked the button it fired. I am not
sure if you can do what I want to do in a web form or not. I was hoping
that I could have a TextChanged event that would fire when the TextBox
contained four characters (enabling the button).

Is there anyway to do this in a web form?

Thanks.
 
M

Mike Moore [MSFT]

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"></asp:Button>
</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.


--------------------
 
G

Greg Smith

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"
 
M

Mike Moore [MSFT]

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.


--------------------
From: "Greg Smith" <[email protected]>
References: <OA9#[email protected]>
<[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.
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.
event
to am
not
 
G

Greg Smith

Thank you Mike.

"Mike Moore [MSFT]" said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,218
Latest member
GracieDebo

Latest Threads

Top