From: http://www.dnnstudy.com/Default.aspx? Tabid = 52 & CTL = detail & Mid = 403 & id = 1640 http://blog.csdn.net/xujh/
For example, we have three textbox and three buttons on the page, and each button performs different actions. We want to check whether the Enter key is pressed in textbox. If yes, execute different button calls. In textbox1, press the Enter key to execute the button1 action ,......
During the test, I found that the server was to be called. Code The _ dopostback function must be called. However, this function is generated by the system except on the page with the DataGrid control. It does not exist on other pages. (You can view the code from the source file ). In this way, we must manually add the _ dopostback function to Aspx. There are two hidden elements, __eventtarget and _ eventargument, which are required by _ dopostback, actually ,. net uploads the name and parameters of the element that generates the event to ,__ eventtarget and _ eventargument. Then, the submit function of form is called and submitted back to the server. The server will know which control is triggered Based on the passed parameters, and then call the corresponding back-end code of form, then, send the new page back to the client.
The following is my test page. Two methods are used to detect the buttons in textbox.
Webform2.aspx
--------------------------------------------------------
<% @ Page Language = "C #" codebehind = "webform2.aspx. cs" autoeventwireup = "false" inherits = "utf8test. webform2" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> webform2 </title>
<Meta content = "Microsoft Visual Studio. NET 7.1" name = "generator">
<Meta content = "C #" name = "code_language">
<Meta content =" Javascript "Name =" vs_defaultclientscript ">
<Meta content = "http://schemas.microsoft.com/intellisense/ie5"; name = "vs_targetschema">
<Script language =" Javascript "Event =" Onkey Down "for =" textbox1 ">
If (event. keycode = 13) <! -- Note case sensitivity -->
{
_ Dopostback ('button1 ','');
Return false; <! -- Very important. Otherwise, you will select button1 for submission. -->
}
</SCRIPT>
<Script language ="Javascript"Event ="OnkeyDown "for =" textbox2 ">
If (event. keycode = 13)
{
_ Dopostback ('button2 ','');
Return false; <! -- Very important. Otherwise, you will select button1 for submission. -->
}
</SCRIPT>
<Script language =" Javascript ">
Function keypress ()
{
If (event. keycode = 13)
{
_ Dopostback ('button3 ','');
Event. keycode = 0; <! -- Very important. Otherwise, you will select button1 for submission. -->
Return false; <! -- Very important. Otherwise, you will select button1 for submission. -->
}
}
</SCRIPT>
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Input type = "hidden" name = "_ eventtarget"> <input type = "hidden" name = "_ eventargument">
<Script language =" Javascript "Type =" text/ Javascript ">
<! --
Function _ dopostback (eventtarget, eventargument ){
VaR theform;
If (window. Navigator. appname. tolowercase (). indexof ("Microsoft")>-1 ){
Theform = Document. form1;
}
Else {
Theform = Document. Forms ["form1"];
}
Theform. _ eventtarget. Value = Eventtarget. Split ("$"). Join (":");
Theform. _ eventargument. Value = Eventargument;
Theform. Submit ();
}
// -->
</SCRIPT>
<Asp: button id = "button1" style = "Z-INDEX: 101; left: 192px; position: absolute; top: 88px" runat = "server"
TEXT = "button1"> </ASP: button> <asp: textbox id = "textbox1" style = "Z-INDEX: 102; left: 16px; position: absolute; top: 88px "runat =" server "> </ASP: textbox>
<Asp: button id = "button2" style = "Z-INDEX: 103; left: 192px; position: absolute; top: 120px" runat = "server"
TEXT = "button2"> </ASP: button>
<Asp: textbox id = "textbox2" style = "Z-INDEX: 104; left: 16px; position: absolute; top: 120px" runat = "server"> </ASP: textbox>
<Asp: textbox id = "textbox3" style = "Z-INDEX: 105; left: 16px; position: absolute; top: 152px" runat = "server"> </ASP: textbox>
<Asp: button id = "button3" style = "Z-INDEX: 106; left: 192px; position: absolute; top: 152px" runat = "server"
TEXT = "button3"> </ASP: button>
<Asp: Label id = "label1" style = "Z-INDEX: 107; left: 24px; position: absolute; top: 56px" runat = "server"> </ASP: label> </form>
</Body>
</Html>
Webform2.aspx. CS
----------------------------------------------------------------------
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Namespace utf8test
{
/// <Summary>
/// Summary of webform2.
/// </Summary>
Public class webform2: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. textbox textbox1;
Protected system. Web. UI. webcontrols. Button button2;
Protected system. Web. UI. webcontrols. textbox textbox2;
Protected system. Web. UI. webcontrols. textbox textbox3;
Protected system. Web. UI. webcontrols. Button button3;
Protected system. Web. UI. webcontrols. Label label1;
Protected system. Web. UI. webcontrols. Button button1;
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
Textbox3.attributes. Add (" Onkey Press "," keypress () "); // case sensitive
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. button1.click + = new system. eventhandler (this. button#click );
This. button2.click + = new system. eventhandler (this. button2_click );
This. button3.click + = new system. eventhandler (this. button3_click );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Private void button#click (Object sender, system. eventargs E)
{
Label1.text = "1 ";
}
Private void button2_click (Object sender, system. eventargs E)
{
Label1.text = "2 ";
}
Private void button3_click (Object sender, system. eventargs E)
{
Label1.text = "3 ";
}
}
}