I am in charge of the project. There is a page with two buttons and two text boxes. When Logging On, I press the Enter key to trigger the "login" event. However, when I search for the Enter key, I also trigger the "login" event, in fact, a "Search event" should be triggered ". In fact, no matter which text box you press enter, the event of the first button is always triggered (this is because the first button event is triggered by the Return key by default ).
Boss asked me to modify it. I think this simple thing is not a difficult problem. After some intense battles, countless brain cells are sacrificed, but there is no progress ,.... it's very hot .... finally, I found a solution with the help of Baidu. I will share the following: [instance]
There are three key points:
1. the login text box is an HTML control, and the search is an Asp.net control. The method for adding events is different;
2. Set attributes for the button control: usesubmitbehavior = "false". Change the type of the button from submit to button;
3. Get the ID of the button control: Document. getelementbyid ('<% = imglogin. clientid %> ')
The instance code is as follows. You are welcome to make a picture...
Front-end code:
View code
1 <% @ page Language = "C #" autoeventwireup = "true" codefile = "ugly. aspx. cs" inherits = "ugly" %>
2
3 <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <HTML xmlns = "http://www.w3.org/1999/xhtml">
5 6 <title> </title>
7 <SCRIPT type = "text/JavaScript">
8
9 function t_login (){
10 if (event. keycode = 13 ){
11 document. getelementbyid ('<% = imglogin. clientid %>'). onclick (); // pay attention to the ID of this place,
12}
13}
14
15 function search (){
16 if (event. keycode = 13 ){
17 document. getelementbyid ('<% = imgsearch. clientid %>'). onclick ();
18}
19}
20
21 </SCRIPT>
22
23 24 <body>
25 <Form ID = "form1" runat = "server">
26 <div>
27 login:
28 <input name = "textfield" type = "text" id = "txtusername" runat = "server" onkeydown = "t_login ();"/>
29
30 <asp: button id = "imglogin" runat = "server" onclick = "imglogin_click"
31 usesubmitbehavior = "false" text = "login"/>
32 <br/>
33 search:
34 <asp: textbox runat = "server" id = "txtkey"/>
35
36 <asp: button id = "imgsearch" runat = "server" onclick = "imgsearch_click"
37 usesubmitbehavior = "false" text = "Search"/>
38 </div>
39 </form>
40 </body>
41
Background code:
View code
1 using system;
2
3 Public partial class ugly: system. Web. UI. Page
4 {
5 protected void page_load (Object sender, eventargs E)
6 {
7 // the text box for logon is the HTML control and the search is the Asp.net control. The method for adding an event to a text box is different.
8 txtkey. Attributes. Add ("onkeydown", "Search ();");
9}
10
11 protected void imglogin_click (Object sender, eventargs E)
12 {
13 // log on...
14 if (txtusername. value! = "")
15 {
16 response. Write ("login ...");
17}
18}
19
20 protected void imgsearch_click (Object sender, eventargs E)
21 {
22 // search
23 if (txtkey. Text! = "")
24 {
25 response. Write ("search ...");
26}
27}
28}