473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

ASP.net - Understanding the Postback

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.
Nov 29 '05 #1
8 1624
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
Nov 29 '05 #2
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.

Nov 29 '05 #3
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.
"Cor Ligthert [MVP]" wrote:
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

Nov 29 '05 #4
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.

"vishal" wrote:
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.

Nov 29 '05 #5
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

Nov 30 '05 #6
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
Nov 30 '05 #7
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.

"Cor Ligthert [MVP]" wrote:
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

Nov 30 '05 #8
You are complaining about a Microsoft standard. You don't have the problem
with PHP.
--
Mad Miss
"David" wrote:
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.

"Cor Ligthert [MVP]" wrote:
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

Dec 2 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matthew Louden | last post by:
I want to know if the PostBack concept applies to HTML web-based forms, regardless of what programming technologies we use: For example, ASP, ASP.NET, Java, CGI, etc... PostBack means to send...
8
by: walesboy | last post by:
greetings - I have a btnSubmit button with a Handles btnSubmit.click which works great if all the user does is click that button. But, if the user ALSO changes a text box on the page (which...
2
by: rodchar | last post by:
hey all, i have a simple app that references the web.config. i browse to the page. i changed the web.config. i do a postback to the page, And session is gone. Only after i close the browser...
4
by: Jim Hammond | last post by:
It would be udeful to be able to get the current on-screen values from a FormView that is databound to an ObjectDataSource by using a callback instead of a postback. For example: public void...
2
by: Wizzard | last post by:
I have a repeater with and imagebutton on a page useing VS2005 ASP.Net 2.0 <asp:Repeater ID="Repeater1" runat="server" > <ItemTemplate> <div> <asp:ImageButton ImageUrl="button.gif"...
11
by: antonyliu2002 | last post by:
I know that this has been asked and answered thousands of times. As a matter of fact, I know that I need to say If Not Page.IsPostBack Then 'Do something End If for things that needs to be...
2
by: Nathan Sokalski | last post by:
I have a DataList in which the ItemTemplate contains two Button controls that use EventBubbling. When I click either of them I receive the following error: Server Error in '/' Application....
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
9
by: 200dogz | last post by:
Hi guys, I want to have a button which opens up a new window when pressed. <asp:Button ID="Button1" runat="server" Text="Open new window" /> ... Button1.Attributes.Add("OnClick",
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.