ASP.net - Understanding the Postback

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a problem which I have worked around for some time. I would appreciate
any comments on the following statements.

When the enter key is pressed it causes a postback. After postback code has
been executed, any event driven code is executed. The most common event is
caused by a mouse click of a button. After the enter key has been pressed,
ASP seems to recognize any event and executes the associated code -
regardless of lack of mouse click or the intentions of the user or system
designer.

The most common button causes the entered detail to be validated and saved -
so an accidental Enter key press causes a few messages about required detail
missing. Most people have experienced this - we are left a bit puzzled
because we are still entering detail, but continue doing so.

The situation is made much more difficult if there is more than one button
on the form. The enter key could cause a rediretion or any number of
unexpected actions.
It may be that only one event is recognized - so if the "Save" button is
placed logically first on the firm, its event will be the only one that is
triggered when the enter key is pressed.

It seems to me to be a deficiency in ASP. I tried to overcome the problem by
using HTML buttons and JavaScript events that set flags. Java script will not
recognize a press of the enter key as an event associated with a click of a
button. The difficulty is that the HTML buttons still have to cause a
postback, requiring the Javascript command __dopostback. It worked perfectly
on two of my three forms. I still do not understand why it would not work on
the third form. I appreciate that it has to be associated with a control
which is capable of causing postback, and believe me I tried everything to
get it to work.

So my users have to wake up to themselves. This is the WWW. If they use the
enter key the detail on my form is going to be processed, and if they get
confusing messages or accidently shunt themselves off into cyberspace, that
is not my problem.
 
David,
I have a problem which I have worked around for some time. I would
appreciate
any comments on the following statements.

When the enter key is pressed it causes a postback.

Can you show us how you do this, I have to create a piece of JavaScript to
force this.

Cor
 
How is possible David that all the events are being executed in the
page???
Only the pageload event and the code for the corresponding button will
be executed at the backend in asp.net.
 
Below is the HTML from simple form to demonstrate my point. It consists of
three text boxes, a label and a button. Code behind the button click event
will place "Button1 click event code executed" into the Label.
When you enter anything into the first text box, then Press ENTER (DON'T
PRESS TAB, don't enter the remaining text boxes), "Button1 click event code
executed" will appear in the label because the event code has been executed.

I have to create complex forms with many controls and more than one event.
As far as my clients are concerned, it was an accidental enter key press, and
the consequences ridicule the application.

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
Inherits="TestEnterKey.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</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">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
<P>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox3" runat="server"></asp:TextBox></P>
<P>
<asp:Label id="Label1" runat="server">Label</asp:Label></P>
<P>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
</form>
</body>
</HTML>

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Label1.Text = "Button1 click event code executed"
End Sub
Thanks for your interest, I appreciate it.
 
Yes there is a bit of misleading Phrasing there.
"The enter key could cause a rediretion or any number of unexpected actions.
It may be that only one event is recognized....". Perhaps you could confirm
that only one event code can be executed. However the point is that a press
of the enter key causes event code to execute, and that enter key action is a
natrual habit of the user.
 
Ok u dont want the textbox2 and textbox3 to be empty.
Then why dont u go with validation controls available in asp.net and
use the code
like
if page.isvalid ....
........
end if
 
David,

What happens, you can see it yourself, is that if you enter something in the
textbox, the first button gets automaticly focus.

Therefore the Enter will affect that button.

You can try it yourself by adding an extra label and button on your sample.

Cor
 
Than you Cor. Yes that is the mechanics of the problem. Lets add it to the
definition of the problem. If the Enter Key is pressed, the first button gets
the focus, causing a post back and executing code behind the button. When the
user's Enter key press is "accidental", the code executed is unexpected and
usually causes confusion.
Vishal, who is also replying to this post, mentions validation controls. An
unexpected postback causes validation controls to display their error
messages. This happens on Microsft's own sites. An innocent Enter key press
after entering one of the first fields causes messages against all required
fields. And it is because the enter key moved the focus to a button which is
able to cause postback.
 

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

Back
Top