Microsoft Knowledge Base Email Alertz

(813822) - Describes a problem where the first enabled Button control receives the RaisePostBack event unexpectedly on the server side when the ENTER key is pressed on the client side.

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 813822 - Last Review: April 19, 2004 - Revision: 1.3

BUG: The first enabled Button control receives the RaisePostBack event when the ENTER key is pressed

SYMPTOMS

After a TextBox control handles the RaisePostBack event, the first enabled Button control also receives the RaisePostBack event.

CAUSE

When a Microsoft ASP.NET page has several Button controls, followed by a TextBox control, followed by more Button controls after the TextBox control, when you enter text in the text box and then you press ENTER, the TextBox control receives the RaisePostBack event. The first enabled Button control will always get the RaisePostBack event after the ENTER key has been pressed on the client side even though the ENTER key that is pressed is handled by another control.

WORKAROUND

To work around this problem, insert the following code in your ASP.NET page, and then modify it to fit your particular application requirements.

You cannot call the Page.IsValid property before validation has occured. Query the Page.IsValid property in the event handler for a control when the value of the CausesValidation property is True or after the Page.IsValid method has been called.
<%@ page  %>

<script runat=server language=c#>

public void Page_Load(object s, EventArgs e)
{
	//Validate();
	if (Page.IsValid)
		Response.Write("Valid");
}

</script>

<form runat=server>

<input type=text runat=server id=mytext>
<input type=submit runat=server>

        <asp:RequiredFieldValidator id="RequiredFieldValidator1"
            ControlToValidate="mytext"
            Display="Static"
            InitialValue="" Width="100%" runat=server>
            Fill in a value!
        </asp:RequiredFieldValidator>
</form>

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

APPLIES TO
  • Microsoft ASP.NET 1.1
  • Microsoft ASP.NET 1.0
Keywords: 
kbbug KB813822
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Anonymous User
Written: 4/27/2004 4:01 AM
Use rather: <script language="Javascript"> function testForEnter() { if (event.keyCode == 13) { event.cancelBubble = true; event.returnValue = false; //form.btnSubmit.click(); } } And place on your textboxes onKeyDown="testForEnter();"

Anonymous User
Written: 4/27/2004 4:27 AM
further clear example would be better the last user's work round seems better!

Anonymous User
Written: 4/27/2004 6:20 AM
Any Suggestions for the problem i am facing in Asp.Net I generate TableGrid ||r to DataGrid at runtime i put a edit button in each row and corresponding data in that row . i also add an event for the button in code behind. when i click on the button the event handler is not raized . But if i regenerate the tablegrid the event fires. is there any way of raizing the button event without regenerating the tblgrid since i was not able paste the whole code.ppl can send me a mail and i will send the code. my mail id is suhas@dacafe.com.

Anonymous User
Written: 4/27/2004 7:29 AM
Example is not clear. While the workround is talking about Page.IsValid property in the event handler for a control. The example shows in Page load.

Syed Jamshaid Akhter - shahjee7 NOSPAM-AT-NOSPAM wol.net.pk
Written: 4/27/2004 9:55 AM
I faced the same problem , but resolved it through passing another extra parameter to find out this from which control this hit has been occured

Wayne Sellars - wsellars NOSPAM-AT-NOSPAM cfwebmasters.com
Written: 4/27/2004 10:43 AM
This works better, this person did a killer job on fixing this. The above example requires manipulation of the validator control. http://www.metabuilders.com/Tools/DefaultButtons.aspx

Ray Cheung - cheura NOSPAM-AT-NOSPAM canada.com
Written: 4/27/2004 3:19 PM
Paste the following javascript at the beginning |html| of your html or aspx file. This will disable the enter key and you can tab around the textbox and press the input button any time you like. Hope this can help.
<script language="JavaScript" type="text/javascript">
 function checkCR(evt) {
   var evt  = (evt) ? evt : ((event) ? event : null);
   var node = (evt.target)?evt.target:((evt.srcElement)?evt.srcElement: null);
    if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
   }
  document.onkeypress = checkCR;
</script>

Anonymous User
Written: 4/27/2004 3:56 PM
I don't understand how any of these solutions will allow me to handle server-side code in the textbox_onclick event.

Tom
Written: 4/30/2004 1:50 PM
Why when everytime I start Windowa XP the moniter settings are changed.How can I keep them as I want?

Shaji Reported as Irrelevant  
Written: 5/4/2004 11:21 PM
If you know which button should handle the default Enter event then do this and it will work. in Page Load event put the following code in. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here Page.RegisterHiddenField("_EVENTTARGET", "btnOK") .....Rest of the code to follow.

whlim Report As Irrelevant  
Written: 10/28/2004 6:04 AM
I face the same problem with radio button instead of text boxes, anyone know the solution?