开发者

Button OnClick event not firing with one textbox in the form

开发者 https://www.devze.com 2023-02-11 04:26 出处:网络
I came across a very strange occurrence with ASP.NET onclick event in IE (other browsers doesn\'t seem to hav开发者_Go百科e this problem). When there is only one textbox field in a form, the onclick e

I came across a very strange occurrence with ASP.NET onclick event in IE (other browsers doesn't seem to hav开发者_Go百科e this problem). When there is only one textbox field in a form, the onclick event doesn't fire when you enter text and hit Enter/Return. The event does fire when you click on the actual submit button. When there are two or more fields in the form, hitting Enter/Return works just fine.

It's very strange. Thought it was something with my IE, but checked other machines, and every one of them had the same problem.

I had setup this really simple page as a test:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Response.Write(((Button)sender).Text);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txtBox1" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtBox2" runat="server" ></asp:TextBox>
    <asp:Button ID="btnSubmit" OnClick="btnSubmit_Click" Text="Submit" runat="server" />
    </div>
    </form>
</body>
</html>

When one of the textbox is removed, the btnSubmit_Click event doesn't behave as expected.


The defaultButton property of Panel or Form is used to ensure Enter Key Submission. Typically people used to Enter Key submission and if it is disabled, it might be annoying certain times.

The defaultButton really provides a way to handle Enter Keys for respective portions of the page when the focus is there.

Source: multiple submit buttons and the enter key

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号