ASP. NET enter the OnClick event solution that triggers the button

Source: Internet
Author: User

There is a TextBox text box on the ASPX page, and there are three button buttons. Enables the TextBox's TextChanged event and the button's Click event.

Problem:

The TextChanged event is now triggered when the TextBox text box has entered the data by pressing "enter", but it also triggers the Click event of the First button button on the page.

Workaround One:

When you hit enter in the text box, the form is submitted. Because a page in. NET has only one form form. So there is a sumbit event.

When the Usesubmitbehavior property is true, the form's commit triggers this event.

The order is to trigger the Click event First and then trigger the button's click time of Usesubmitbehavior to true.

Of course, if there are multiple buttons, the first button event is triggered by default.

To resolve this problem, change the Usesubmitbehavior to false. (When multiple buttons, the Usesubmitbehavior is all changed to false.) )

Workaround Two:

(When you want to use the button's OnClientClick pop-up Confirmation window, if Usesubmitbehavior is false, regardless of what you choose not to execute the server button's onclick code, this time you need to use the workaround two)

The solution comes from the e-chapter of Mencius:

private void Page_Load (object sender, System.EventArgs e)
{   
TextBox1.Attributes.Add ("onkeydown", "if (event.keycode==13) {This.blur (); return false}");
}   
private void Button1_Click (object sender, System.EventArgs e)
{   
Response.Write ("button");
}   
private void textBox1_TextChanged (object sender, System.EventArgs e)
{Response.Write (TextBox1.Text);
}

(Ensure that the AutoPostBack property of TextBox1 is true, as long as it is set to true, the above code is not required to resolve the problem.) )

Alternatively, you can set the client Id.focus () or set the TabIndex property.

ASP. NET enter the OnClick event solution that triggers the button

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.