Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Trapping Enter Keypress
Message
 
To
13/10/2006 16:06:09
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01161896
Message ID:
01161980
Views:
18
Hi Mike,

This post doesn't add any value to the excellent response Keith has given, but I would like to comment on some ASP.NET behavior I had to deal some time ago that might be of interest. Please, consider the simple ASPX page bellow (no code-behind for the sake of simplicity):

Ah, all the information here is only valid for ASP.NET 2.0, since ASP.NET 1 had some weird bugs regarding default buttons :)
< %@ Page Language="C#" AutoEventWireup="true" % >

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
    <title>Untitled Page</title>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            _lbl1.Text = _txt1.Text;
        }
    </script>
    
</head>

<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="_txt1" runat="server" />
        <asp:Button ID="_btn1" runat="server" Text="Button1"/>
        <asp:Button ID="_btn2" runat="server" text="Button2"/>
        <asp:Label ID="_lbl1" runat="server" />
    </form>
</body>

</html>
Pressing ENTER on this page should not cause any post at all (I have tested with IE and Firefox only).

Now, If you add the defaultbutton="_btn1" attribute within the "form" tag, pressing ENTER should use that specific button as de default button and submit the form as if you have actually clicked that button using the mouse.
    <form id="form1" runat="server" defaultbutton="_btn1">
You can also have different default buttons on the same page. But for this to work, you will have to put them in different asp:Panel controls. Each panel can have its own default button which submits the form when that panel has the focus and the user presses ENTER. The page below shows this behavior:
< %@ Page Language="C#" AutoEventWireup="true" % >

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
    <title>Untitled Page</title>
    <script runat="server">
        protected void ShowText1(object sender, EventArgs e)
        {
            _lbl1.Text = _txt1.Text;
        }
        protected void ShowText2(object sender, EventArgs e)
        {
            _lbl2.Text = _txt2.Text;
        }
    </script>
    
</head>

<body>
    <form id="form1" runat="server">
        <asp:Panel ID="Panel1" runat="server" DefaultButton="_btn1">
            <asp:TextBox ID="_txt1" runat="server" />
            <asp:Button ID="_btn1" runat="server" Text="Button1" OnClick="ShowText1" />
            <asp:Button ID="_btn2" runat="server" text="Button2" OnClick="ShowText1" />
            <asp:Label ID="_lbl1" runat="server" />
        </asp:Panel>
        <hr />
        <asp:Panel ID="Panel2" runat="server" DefaultButton="_btn4">
            <asp:TextBox ID="_txt2" runat="server" />
            <asp:Button ID="_btn3" runat="server" Text="Button3" OnClick="ShowText2" />
            <asp:Button ID="_btn4" runat="server" text="Button4" OnClick="ShowText2" />
            <asp:Label ID="_lbl2" runat="server" />
        </asp:Panel>
    </form>
</body>

</html>
Sorry if I missed the track of the discussion, but I had lots of problems in the past (specially with VS 2003) regarding submits and default buttons, so I thought that this would help in some way.

Cheers!
-----
Fabio Vazquez
http://www.fabiovazquez.com
Previous
Reply
Map
View

Click here to load this message in the networking platform