ASP. NET 2.0-Enter key-default submit button

Source: Internet
Author: User
One of the most annoying tasks in Web development is to process "Enter key" and "Enter key" for forms. Although we provide users with the submit button, the simplest and most direct way is to enter the text and press enter to complete the submission.

ASP. NET 2.0 provides a good solution for this problem. You only need to specify the "defaultbutton" attribute to the ID of the button control to be triggered.

You can specify "defaultbutton" at the form level and panel level (<asp: Panel> tag ". If defaultbutton is specified in both the form and panel, if "Enter key" is triggered in the panel

The following instanceCodeThere is a form and four panels, and there are buttons in the report and panel. Note: Which button events will be triggered when you press enter in the text box?

<Form ID = "form1" runat = "server" defaultbutton = "btn1">

<Div>

<Asp: textbox id = "TXT" runat = "server"> </ASP: textbox>

<Asp: button id = "button5" runat = "server" text = "cancel" onclick = "button5_click"/>

<Asp: button id = "btn1" runat = "server" text = "Submit" onclick = "btn1_click"/>

<Asp: Panel id = "pnl1" runat = "server" defaultbutton = "button1">

<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>

<Asp: textbox id = "textbox2" runat = "server"> </ASP: textbox>

<Asp: button id = "button1" runat = "server" text = "button1" onclick = "button#click"/>

</ASP: Panel>

<Asp: Panel id = "Panel1" runat = "server" defaultbutton = "button2">

<Asp: textbox id = "textbox3" runat = "server"> </ASP: textbox>

<Asp: textbox id = "textbox4" runat = "server"> </ASP: textbox>

<Asp: button id = "button2" runat = "server" text = "button2" onclick = "button2_click"/>

</ASP: Panel>


<Asp: Panel id = "panel2" runat = "server" defaultbutton = "button3">


<Asp: textbox id = "textbox5" runat = "server"> </ASP: textbox>

<Asp: textbox id = "textbox6" runat = "server"> </ASP: textbox>

<Asp: button id = "button3" runat = "server" text = "button3" onclick = "button3_click"/>

</ASP: Panel>

<Asp: Panel id = "panel3" runat = "server" defaultbutton = "button4">

<Asp: textbox id = "textbox7" runat = "server"> </ASP: textbox>

<Asp: textbox id = "textbox8" runat = "server"> </ASP: textbox>

<Asp: button id = "button4" runat = "server" text = "button4" onclick = "button4_click"/>

</ASP: Panel>

</Div>

</Form>

The corresponding, sample events for the button clicks are

Protected void button#click (Object sender, eventargs E)
{
Response. Write (button1.text );
}

Protected void button2_click (Object sender, eventargs E)
{
Response. Write (button2.text );
}

Protected void button3_click (Object sender, eventargs E)
{
Response. Write (button3.text );
}

Protected void button4_click (Object sender, eventargs E)
{
Response. Write (button4.text );
}

Protected void btn1_click (Object sender, eventargs E)
{
Response. Write (btn1.text );
}

Protected void button5_click (Object sender, eventargs E)
{
Response. Write (button5.text );
}

Original article: http://forums.asp.net/thread/1270048.aspx

Original article: http://forums.asp.net/thread/1270048.aspx
Code download

I really don't know how to translate "Entry key". Please advise.
Thank you.

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.