Asp.net: One way to set the default button (041217 update)

Source: Internet
Author: User

041217 make some necessary changes to defaultbutton. js.

Because the buttons in Asp.net are of the submit type, pressing the carriage return in the form triggers the click event of the first button, if multiple buttons exist, the subsequent button events cannot be triggered when you press Enter. In the following solution, you can change the default button of the page through settings.

1. Add supportdefaultbutton () to the load event on the base page ()

2. Declare variables on the base page:

Protected defaultbutton as button

3. Add the supportdefaultbutton function to the base page and reload raisepostbackevent.

Private   Sub supportultbutton () SubSupportdefaultbutton ()

Getpostbackeventreference (NewSystem. Web. UI. Control)

Registerstartupscript ("Defaultbutton","<Script language = 'javascript 'src = '/JS/defaultbutton. js'> </SCRIPT>")

End sub


Protected   Overloads   Overrides   Sub raisepostbackevent () Sub Raisepostbackevent ( Byval Sourcecontrol As System. Web. UI. ipostbackeventhandler, Byval Eventargument As   String )

If (Defaultbutton Is   Nothing   Orelse Request ( " _ Eventtarget " ) <>   "" ) Then

Mybase . Raisepostbackevent (sourcecontrol, eventargument)

Elseif ( Not Defaultbutton Is   Nothing ) Then

Mybase . Raisepostbackevent ( Directcast (Defaultbutton, system. Web. UI. ipostbackeventhandler ), Nothing )

End   If

End sub

4. defaultbutton. js file content:
For (I = 0 ; I < Form1.elements. length; I ++ )

{

If (Form1.elements [I]. Type =   " Submit " )

{

Fstsubmit = Form1.elements [I]

Fstsubmit. attachevent ( " Onmouseup " , Changeevent)

// Break ; This sentence should be removed

}

}

Function Changeevent ()

{

_ Dopostback (event. srcelement. ID ,'')

}

In the load event of the inherited page:

5. defaultbutton = Name of the object to be set as the default button

You can.

IE6 passed the test. The earlier version was not tested, but it should be okay. If you are interested, you can test the compatibility. If there is a better way, please also give a link to see.

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.