Asp. NET Enter COMMIT Transaction

Source: Internet
Author: User
Tags html form net return

An analysis of ASP. NET return commit event [go]

Asp. NET Enter commit event is not actually an ASP, but is a concrete discussion of how the Submit button in HTML form is planned. can also be attributed to part of ASP. What is the implementation of the ASP. NET return commit event? Let's look at the following specific:

Asp. NET Enter commit event implementation 1,

When your cursor focus enters a FORM element, it activates the first (if any) button in the form that Type=submit (flow layout obeys left to right, top to bottom), waits for the response carriage return event, and submits the form

You can test the code:

  1. ﹤form action= "" ﹥
  2. ﹤input type= "Text" /﹥
  3. ﹤input type= "Submit" value= "Submit" /﹥
  4. ﹤/form﹥
  5. ﹤form action= "" ﹥
  6. ﹤input type= "Text" /﹥
  7. ﹤input type= "button" value= "Submit" /﹥
  8. ﹤/form﹥

Asp. NET Enter commit event implementation 2.

button is rendered as ﹤input by default in ASP. Type=submit﹥ at this point, no additional script is submitted for form, the Submit button is designed to submit the form and is rendered as ﹤input in 1.x Type=button onc Lick=_dopostback (...)/﹥ this normal button does not have the above default behavior of Submit

Asp. NET Enter commit event implementation 3. Disabling this default behavior has two methods

(1) Set the Defualtbutton of the form element as the real button you want to respond to the carriage return as follows

    1. ﹤form id= "Form1"    
    2. runat= "Server"    
    3. defaultbutton= "Button1" ﹥

Note DefaultButton =﹤﹤targetbutton.id﹥﹥ therefore this may not be valid for a Button in a composite control such as a template (not tested)

(2) Modify the button rendering mode usesubmitbehavior= "false"

    1. ﹤asp:button id= "Button1"    
    2. runat= "Server" text= "button"    
    3. onclick= "Button1_Click"    
    4. usesubmitbehavior= "false" /﹥

Alternatively, you can filter the enter practice by controlling the focus, and you need to record the ID of the control that contains the current page focus:

    1. Document.activeelement

For ASP. After you enter content in TextBox1, press ENTER to execute the Button1 click Method. Then write in the Page_Load event method.

  1. TextBox1.Attributes.Add ("onkeydown",
  2. " if (event. which | | Event . KeyCode) {  
  3. if (event. which = = 13) | | (Event. keycode = =)) {
  4. document.getElementById (' "+
  5. button1.uniqueid+ "'). Click (); return    false;}}
  6. Else {return true};  ");

There is an ASP. NET inside the form runat=server forms, the inside of the ﹤asp:button. Can not press a return to submit the form, it is very uncomfortable.
Now finally found a property can do this thing, with this. Form.defaultbutton = "Contentplaceholder1$btsubmit";
It is important to note that if MasterPage (Master page) is used, the id:contentplaceholderid of the motherboard and a dollar symbol ($) are added before the button ID.

The C # implementation code is as follows:

  1. ﹤%@ Page language= "C #"    
  2. masterpagefile= "~/masterpage.master"    
  3. autoeventwireup= "true"    
  4. codefile= "Login.aspx.cs"    
  5. inherits= "Login" %﹥
  6. ﹤asp:content id= "Content1"    
  7. contentplaceholderid= "ContentPlaceHolder1"    
  8. runat= "Server" ﹥
  9. ﹤asp:textbox runat= "Server"    
  10. id= "WD" ﹥﹤/asp:textbox﹥
  11. ﹤asp:button id= "Btsubmit"    
  12. runat= "Server" text= "Submit"  
  13. onclick= "Btsubmit_click" /﹥
  14. ...........................
  15. ﹤/asp:content﹥

(1) In the class containing the motherboard page:

    1. protected void Page_Load (object sender, EventArgs e)
    2. {
    3. This .  Form.defaultbutton = "Contentplaceholder1$btsubmit";
    4. }

Or on the content page

    1. protected void Page_Load (object sender, EventArgs e)
    2. {
    3. This .  Page.Form.DefaultButton = "Contentplaceholder1$btsubmit";
    4. }

(2) In a class that is not a motherboard page:

    1. protected void Page_Load (object sender, EventArgs e)
    2. {
    3. This .  Form.defaultbutton = "Btsubmit";
    4. }

Asp. NET enter the details of the submission of the event to introduce you here, I hope you understand and learn ASP.

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.