This article is excerpted from the vertical entry of ASP. NET 3.5 Control and component development technology.
For server-side control elements, such as the button standard service-side control of ASP., you can automatically send requests to the service-side processing at commit time, so that we do not have to handle their event postbacks ourselves, but for rendering HTML elements that do not cause a postback, such as a text box (textbox) or Link button (LinkButton), and you want the control to start a postback, you can do this by programming in ASP. NET in an event structure that relies on client-side scripting.
A postback and snapping are also required to handle an event in its entirety. Capture is the IPostBackEventHandler interface, the previous section is more clear, this section is mainly about the postback (client postback request to the server).
The following is a common HTML tag to expand the parsing client postback mechanism, as well as various HTML markup postback form.
5.2.2.1 sets the type of HTML button tag to submit
The example of the Postbackeventcontrol control in the previous section is to submit the postback from the client in the same way that the type of the HTML button tag is set. The postback code is as follows:
Output. Write ("<input type=submit name=" + this. UniqueID + "value= ' click I '/>");
Input is a standard HTML markup control that does not runat= "server" by default, and these controls can only handle some client methods by default. The Type property of input represents the control types of the tag, such as: Type=button represents a button, Type=submit represents a button that submits form functionality, Type=text represents a text box control, Type=file represents an upload file control, and so on. The main explanation here is that when the Type=submit type of control represents the Submit button, it displays the same style as the Type=button, unlike when you click it to submit the form to the server, and no additional code is required for the ASP. NET is to submit the form to the current page by default. The Type=submit submission feature is supported from IE version 3.0, and can be used as long as the browser is not less than this version.
5.2.2.2 using method getpostbackeventreference to get postback script
1. Add postback functionality to HTML client controls
The buttons in the General page are not all type=submit types, most of them are Type=button type buttons, the Type=submit in the code snippet above 5.2.2.1 is changed to Type=button, and the modified code is as follows:
Output. Write ("<input Type=button name=" + this. UniqueID + "value=" [use submit button] '/> ');
Since the Type property of the button in this code snippet is changed from Sumbit to the General button type, the button will no longer have the ability to post back to the service side when it is output. In order to make the general button also has the function of postback, ASP. NET provides the Page.ClientScript.GetPostBackEventReference method. The ClientScript type is ClientScriptManager, and the primary feature of this class is to define a method for managing client script in a Web application. The Getpostbackevent reference method body structure is as follows:
GetCallbackEventReference (String, String, String, String, String, Boolean)
The function of this method is to get a reference to a client function, and when called, initiates a client callback to the server event. The client function for this overloaded method contains the specified target, parameter, client script, context, error handler, and Boolean value.
You can use the ClientScriptManager class to invoke a client callback when you expect to run server code from the client without performing a postback. This is called performing an out-of-band callback on the server. In a client callback, the client script function sends an asynchronous request to an ASP. The Web page modifies its normal life cycle to handle callbacks. Use the Getcallbackevent reference method to get a reference to a client function that initiates a client callback to the server-side event when the function is called.
Using the GetCallbackEventReference method to add callback client functionality to the above code, the revised code is as follows:
Output. Write ("<input Type=button name=/" {0}/"value=" [Use Page.clientscript object method] ' onclick=/' {1}/> ', this. UniqueID, Page.ClientScript.GetPostBackEvent Reference (This, ""));
The first parameter of the Page.ClientScript.GetPostBackEventReference method passes the current control reference, which can be passed to any control reference in the actual application, including child controls, and the second parameter is an optional command parameter, which is set to NULL, which can be configured when multiple buttons are processed simultaneously This parameter is a different command name.
In addition, the Page.clientscript object has a very important method getcallbackeventreference. Use the GetCallbackEventReference method to get a reference to a client function, when called, it initiates a client callback to the server-side event, can support setting the client callback method name, and so on, here simply to mention, There is also a special section later that describes the support that ASP. NET control development provides to clients.
Turn to the point where the button's onclick Click event is added to the code, and when the button is clicked, a string of client script returned by the Getpostbackevent reference method is called, and a client method and two Type=hidden hidden field controls are generated in the page. The following is the HTML code rendered to the client by the above code:
<input Type=button name= "PostBackFromClientControl1" value= ' [use page. ClientScript object method] ' onclick= ' __dopostback (' PostBackFromClientControl1 ', ') ' >
<div>
<input type= "hidden" name= "__eventtarget" id= "__eventtarget" value= ""/>
<input type= "hidden" name= "__eventargument" id= "__eventargument" value= ""/>
</div>
<script type= "Text/javascript" >
<! [cdata[
var theform = document.forms[' Form1 '];
if (!theform) {
Theform = Document.form1;
}
function __doPostBack (Eventtarget, eventargument) {
if (!theform.onsubmit | | (Theform.onsubmit ()! = False)) {
Theform.__eventtarget.value = Eventtarget;
Theform.__eventargument.value = eventargument;
Theform.submit ();
}
}
]]>
</script>
As you can see from the HTML source above the final output, the Click event of the button control executes a method named _dopostback, and this method is automatically generated in the page. In the _dopostback method, the event target object Eventtarget (the first argument passed when the GetPostBackEventReference method is called, the current control) is assigned to the current form object. Eventargument the event argument object (the second argument passed when the GetPostBackEventReference method is called), and finally invokes the Form object's submission method, submits the forms, and here it implements the postback function we want.
Also notice the following two lines of autogenerated code:
<input type= "hidden" name= "__eventtarget" id= "__eventtarget" value= ""/>
<input type= "hidden" name= "__eventargument" id= "__eventargument" value= ""/>
This is the two hidden field type control, which is used primarily to store the value of the event target object and the event argument object. Also, between the script tags above, there is a nested//<! [cdata[//]]> clause, which is independent of pure control development techniques. The function of this sentence is to inform the HTML reader that this paragraph is not HTML content, it needs to be saved separately by additional escape characters in case of code parsing error.
2. Generate a postback script for a server-side control
The Page.ClientScript.GetPostBackEventReference method can also generate its own postback script for the server-side control, which can be postback from the client.
Asp. NET Standard, the final HTML markup generated by the button service-side control is nothing more than a control that generates a Type=submit type. For server-side controls, you can also set the client postback feature. For example, if you set the Usesubmitbehavior property to False when you use the button control, the Auto-submit feature of the buttons is disabled, and you can use the GetPostBackEventReference method to return the client postback event script for the button control. The code looks like this:
String Strpostbackcode =
This. Page.ClientScript.GetPostBackEventReference (button1, "edit");
The client event of the button is then associated with the generated postback event script:
this.button1.attributes["onclick"] = Strpostbackcode;
Other server-side controls are also set up as such.
5.2.2.3 using method Getpostbackclienthyperlink to get postback script
This approach is similar in principle to getpostbackeventreference. The function of this method is to get a script reference, a little different from the former is to append a javascript: prefix, which belongs to the basic JavaScript syntax, Often used in non-scripting languages (such as HTML) source code to tell the browser after the prefix of the format string as a JavaScript scripting language to parse, such as: Javascript:alert (' Hello ') is a popup "Hello" dialog box. The reference can be sent back to the server of the specified control in the client event, using the specified event arguments and a Boolean value to indicate whether to register the postback for event validation. The method body structure is as follows:
Getpostbackclienthyperlink (Control, String, Boolean)
Same as GetPostBackEventReference, the first parameter is the event target object, the second argument is an optional parameter, and the third parameter indicates whether the postback event is registered for validation.
Here is an example of an application with the following code:
String href = Page.ClientScript.GetPostBackClientHyperlink (this, "");
Output. AddAttribute (Htmltextwriterattribute.href, Href);
Output. RenderBeginTag (HTMLTEXTWRITERTAG.A);
Output. Write ("[Getpostbackclienthyperlink method using Page.clientscript Object]");
Output. RenderEndTag ();
Unlike before, this is the output of an HTML <a> tag. It is believed that the reader has guessed the application scenario of getpostbackclienthyperlink (by means of the method name ***hyperlink, it is possible to see which control is used specifically for the function). Look directly at the HTML source code of the resulting client:
<a href= "Javascript:__dopostback (' PostBackFromClientControl1 ', ')" >[ Use the Getpostbackclienthyperlink method of the Page.clientscript object]</a>
<div>
<input type= "hidden" name= "__eventtarget" id= "__eventtarget" value= ""/>
<input type= "hidden" name= "__eventargument" id= "__eventargument" value= ""/>
</div>
<script type= "Text/javascript" >
<! [cdata[
var theform = document.forms[' Form1 '];
if (!theform) {
Theform = Document.form1;
}
function __doPostBack (Eventtarget, eventargument) {
if (!theform.onsubmit | | (Theform.onsubmit ()! = False)) {
Theform.__eventtarget.value = Eventtarget;
Theform.__eventargument.value = eventargument;
Theform.submit ();
}
}
]]>
</script>
From the above code can be seen, in addition to this sentence:
href= "Javascript:__dopostback (' PostBackFromClientControl1 ', ')"
The chain contact hair style is somewhat different from the onclick trigger of the previous button control, and the other code is exactly the same.
In addition, ASP. NET automatically processes reusable parts of the code, such as when there are more than one committed element in the page, the public method of handling the page submission _dopostback always generates one in the current page, and no redundancy is generated.
There is so much to be said about the use of the Getpostbackclienthyperlink method. This section focuses on how several common HTML tags call client postbacks and how the client postback works. Note that the client postback event is defined using the GetPostBackEventReference method and the Getpostbackclienthyperlink method. These methods enable client-side scripting functions that, when called, cause the server to postback to the page. The difference between a client postback and a client callback is that the Web page handles the client postback event to run out of a normal life cycle, and GetCallbackEventReference is an asynchronous request, in the client callback, The client script function sends an asynchronous request to an ASP. NET page, and the Web page modifies its normal life cycle to handle callbacks. There are some differences between the two calls.