Asp. NET AJAX-based Enter key submission problem Analysis _ practical skills

Source: Internet
Author: User

The example in this article tells you about ASP.net's Ajax-based enter key submission. Share to everyone for your reference, specific as follows:

Recently encountered a enter key problem, summed up:

1. First review the HTML form of the response to enter, different browsers on enter processing slightly different, here using IE7

A. HTML form has only one textbox, no submit button, click Enter to submit form.
B. >1 textbox, no Submit button, click Enter, form does not respond.
C. Page has one (or more) Submit button, click Enter, trigger the first submit button click.

2. Enter key in asp.net

ASP.net 2.0 provides the DefaultButton property on the form.

Copy Code code as follows:
<form id= "form" runat= "Server" defaultbutton= "DUMMYDEFAULTBTN" >

3. DefaultButton under Master page.

Set in Page_Load in page

protected void Page_Load (object sender, EventArgs e)
{
  if (!this. Page.IsPostBack)
  {this
   . Page.Master.Page.Form.DefaultButton = this. Btnsave.uniqueid;

4. DefaultButton under Ajax

The local refresh of Ajax destroys the DefaultButton implementation.

A. ASP. NET's DefaultButton implementation.

Add the Webform_firedefaultbutton method to the HTML.

Copy Code code as follows:
<form name= "Aspnetform" method= "POST" action= "" onsubmit= "Javascript:return webform_onsubmit ();" Onkeypress= " Javascript:return Webform_firedefaultbutton (event, ' <your btnid> ') "id=" Aspnetform ">

Implementation of Webform_firedefaultbutton:

var __defaultfired = false;
function Webform_firedefaultbutton (event, target) {
 if (!__defaultfired && event.keycode = &&!) ( Event.srcelement && (event.srcElement.tagName.toLowerCase () = = "textarea")) {
  var DefaultButton;
  if (__nonmsdombrowser) {
   DefaultButton = document.getElementById (target);
  }
  else {
   DefaultButton = Document.all[target];
  }
  if (DefaultButton && typeof (Defaultbutton.click)!= "undefined") {
   __defaultfired = true;
   Defaultbutton.click ();
   Event.cancelbubble = true;
   if (event.stoppropagation) event.stoppropagation ();
   return false;
  }
 }
 return true;
}

B. The problem is that the JS variable __defaultfired in AJAX does not update back false, causing the default button to fail.

C. Solutions

The same, set yourself

Copy Code code as follows:
__defaultfired=false;

I hope this article will be helpful to everyone asp.net order design.

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.