Use onmousedown and onclick to make the button only respond to the mouse click event

Source: Internet
Author: User

The purpose is probably the same as the previous articleArticle
Block the response of the button to the enter return key and implement the "Enter-> tab" Effect
Similar
To prevent
Button-to-Car return key and other non-click actions
Unnecessary misoperation caused by Response

=========== Front-end page ================
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default3.aspx. cs" inherits = "default3" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<Script language = "JavaScript" type = "text/JavaScript">
VaR nowmouseclick = "";

Function fn_onmousedown ()
{
Nowmouseclick = event. srcelement. ID;
}

Function fn_onclick ()
{
If (nowmouseclick = event. srcelement. ID)
{
Nowmouseclick = "";
Return true;
}
Else
{
If (event. srcelement. ID = "imagebutton1 ")
{
Document. All. button1.focus ();
}
Else if (event. srcelement. ID = "button1 ")
{
Document. All. text1.focus ();
}
Return false;
}
}

Function fn_text1_onkeydown ()
{
If (event. keycode = 13)
{
Document. All. text2.focus ();
Event. keycode = 0;
Event. returnvalue = false;
}
}
Function fn_text2_onkeydown ()
{
If (event. keycode = 13)
{
Document. All. imagebutton1.focus ();
Event. keycode = 0;
Event. returnvalue = false;
}
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Input id = "text1" type = "text" onkeydown = "fn_text1_onkeydown ();"/>
<Input id = "text2" type = "text" onkeydown = "fn_text2_onkeydown ();"/>
<Br/>
<Asp: imagebutton id = "imagebutton1" runat = "server" onclick = "imagebutton1_click"/>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "button"/>
</Form>
</Body>
</Html>

========= BackgroundCode==================

Public partial class default3: system. web. UI. page
{< br> protected void page_load (Object sender, eventargs e)
{< br> This. imagebutton1.attributes. add ("onmousedown", "fn_onmousedown ();");
This. imagebutton1.attributes. add ("onclick", "Return fn_onclick ();");
This. button1.attributes. add ("onmousedown", "fn_onmousedown ();");
This. button1.attributes. add ("onclick", "Return fn_onclick ();");
}

protected void imagebutton1_click (Object sender, imageclickeventargs e)
{< br> response. write ("imagebutton1 serve event");
}< br> protected void button#click (Object sender, eventargs e)
{< br> response. write ("button1 serve event");
}< BR >}

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.