Client sender/callback and sender callback of ASP. NET 3.5 controls and component development technology
This document is excerpted from ASP. NET 3.5 control and component development technology.
For server control elements, such as ASP. NET Button standard server controls can automatically send requests to the server for processing when they are submitted. Such controls do not have to process their event sending back by ourselves; however, for HTML elements that do not cause sending back, such as "text box" or "link button", if you want the control to start sending back, you can go to ASP. NET program this function by relying on the event structure of client scripts.
To completely process an event, you also need to re-send and capture it. Capture is a task of the IPostBackEventHandler interface. The previous section makes it clear that this section focuses on sending back (sending back requests from the client to the server ).
The following describes some common HTML tags used to analyze the client's sending and response mechanisms, as well as various HTML Tag sending and response forms.
5.2.2.1 set the HTML Button tag type to submit
The PostBackEventControl control in the preceding section uses the method of setting the HTML Button tag type to submit and send back from the client. The return code is as follows:
Output. Write ("<input type = submit name =" + this. UniqueID + "Value = 'click me'/> ");
INPUT is a standard HTML Tag control, which does not have runat = "server" by default, and these controls can only process some client methods by default. The TYPE attribute of INPUT indicates the type of the tag control, for example, type = button indicates the button; type = submit indicates the button for the form submission function; TYPE = text indicates the text box control; type = file indicates the file Upload control. The main note here is that when the type = submit type control represents the submit button, it displays the same style as the type = button, the difference is that after clicking it, you can submit the form to the server without additional code, ASP. by default, the form is submitted to the current page. Type = the submit submission function is supported from IE 3.0, and can be used as long as the browser version is not small.
5.2.2.2 GetPostBackEventReference
1. added the send-back function for the HTML client control.
Generally, buttons on the page are not of the type = submit type. Most of the buttons are of the type = button type. Modify type = submit in the code snippet in section 5.2.2.1 above to type = button, the modified code is as follows:
Output. Write ("<input type = button name =" + this. UniqueID + "Value = '[use submit button]'/> ");
Because the type attribute of the button in this code segment is changed from sumbit to General button type, the button output will no longer have the function of sending back to the server. ASP. NET provides the Page. ClientScript. GetPostBackEventReference method to enable normal buttons to send back. ClientScript is of the ClientScriptManager type. The main function of this type is to define a method for managing client scripts in a Web application. The structure of the GetPostBackEvent Reference method is as follows:
GetCallbackEventReference (String, Boolean)
This method is used to obtain a reference to the client function. When this method is called, a client callback for server events is started. The client function of this overload method contains the specified target, parameter, client script, context, error handler, and Boolean value.
You can use the ClientScriptManager class to call the client callback when you expect to run the server code from the client without sending back. This is called an out-of-band callback to the server. In the client callback, the client script function sends an asynchronous request to the ASP. NET webpage. Modify the normal lifecycle of a webpage to process callback. Use the GetCallbackEvent Reference method to obtain a Reference to the client function. When this function is called, it starts a client callback for server-side events.
Use the GetCallbackEventReference method to add the callback client function for the above Code. The corrected 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 ,""));
Page. clientScript. the first parameter of the GetPostBackEventReference method is used to pass the current control reference. In actual application, any control reference, including sub-controls, can be passed. The second parameter is an optional command parameter, which is set to null, when processing multiple buttons at the same time, you can set this parameter to a different command name.
In addition, the Page. ClientScript object has a very important method, GetCallbackEventReference. Use the GetCallbackEventReference method to obtain a reference to the client function. When this function is called, it starts a client callback for server-side events, and supports setting the client callback method name, here, I will only mention it briefly. There will be a special chapter to introduce ASP later.. NET Control Development supports the client.
Add the onclick event of the button to the Code, and call the client script returned by the GetPostBackEvent Reference method when you click the button, in addition, a client method and two hidden domain controls with type = hidden are generated on the page. The following is the HTML code that the above Code is presented to the client:
<INPUT type = button name = "PostBackFromClientControl1" value = '[use the 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>
From the HTML source code output above, we can see that the Click Event of the Button control executes a method named _ doPostBack, and this method is automatically generated on the page. In the _ doPostBack method, assign the eventTarget object (the first parameter passed when the GetPostBackEventReference method is called, which is the current control) to the current Form object, call the eventArgument (the second parameter passed when the GetPostBackEventReference method is called) of the event parameter object, call the Form object submission method, and submit the Form. The desired sending back function is implemented here.
Note the following two automatically generated codes:
<Input type = "hidden" name = "_ EVENTTARGET" id = "_ EVENTTARGET" value = ""/>
<Input type = "hidden" name = "_ EVENTARGUMENT" id = "_ EVENTARGUMENT" value = ""/>
These are two hidden domain control controls used to store the values of event target objects and event parameter objects. Also, there is a nested // between the above script tags. <! [CDATA [//]> clause, which has nothing to do with pure control development technology. The function of this sentence is to notify the HTML reader that the section below is not HTML content. It needs to be saved separately according to the additional escape characters to prevent code parsing errors.
2. Generate a send-back script for the server control
The Page. ClientScript. GetPostBackEventReference method can also generate its own sending-back script for the server control and send it back from the client.
The HTML Tag generated by the ASP. NET standard Button Server Control is nothing more than a control of the type = submit type. You can also set the client sending back function for the server control. For example, if the UseSubmitBehavior attribute is set to false when the Button control is used, if the automatic submission function of the Button is disabled, the GetPostBackEventReference method can be used to return the client sending event script of the Button control. The Code is as follows:
String strPostBackCode =
This. Page. ClientScript. GetPostBackEventReference (button1, "edit ");
Then, associate the customer event of the Button with the generated send-back event script:
This. button1.Attributes ["onclick"] = strPostBackCode;
This is also true for other server controls.
5.2.2.3 GetPostBackClientHyperlink
The principle of this method is similar to that of GetPostBackEventReference. The function of this method is to obtain a script reference. The difference with the former is that a javascript: prefix is appended at the beginning of the method, which is a basic syntax of JavaScript, it is often used to tell the browser that the format string behind the prefix is parsed as a JavaScript scripting language in the source code of a non-scripting language (such as HTML), such as javascript: alert ('hello ') that is, a "hello" dialog box is displayed. This reference can be sent back to the server of the specified control in the client event. When sending back, the specified event parameter and a Boolean value are used to indicate whether the callback is registered for event verification. The method body structure is as follows:
GetPostBackClientHyperlink (Control, String, Boolean)
Same as GetPostBackEventReference, the first parameter is the event target object, the second parameter is an optional parameter, and the third parameter indicates whether the event is verified and returned.
The following is an example of the application code:
String href = Page. ClientScript. GetPostBackClientHyperlink (this ,"");
Output. AddAttribute (HtmlTextWriterAttribute. Href, href );
Output. RenderBeginTag (HtmlTextWriterTag. );
Output. Write ("[use the GetPostBackClientHyperlink method of the Page. ClientScript object]");
Output. RenderEndTag ();
Different from the previous one, here is an HTML <a> tag. I believe the reader has guessed the Application Scenario of GetPostBackClientHyperlink (through the method name *** Hyperlink, You can see which control it is designed ). Let's take a look at the HTML source code of the final 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 code above, we can see that in addition to this sentence:
Href = "javascript :__ doPostBack ('postbackfromclientcontrol1 ','')"
The link trigger method is different from the onclick trigger method of the previous Button control. Other code is exactly the same.
In addition, ASP. NET will automatically process reusable part of the Code. For example, when there are multiple elements that can be submitted on the page, the common method _ doPostBack for page submission will always generate one on the current page without generating redundancy.
There are so many things about how to use the GetPostBackClientHyperlink method. This section describes how to call the client sending method and the principle of sending back the client using common HTML tags. Note: Use the GetPostBackEventReference method and the GetPostBackClientHyperlink method to define the client sending back event. These methods enable client-side scripting functions. When these functions are called, they will prompt the server to send back to this page. The difference between client-side sending and client-side callback is that a normal life cycle is used up for processing client-side sending events on the webpage, while GetCallbackEventReference is an asynchronous request. In the client-side callback, the client-side script function sends a request to ASP.. NET web pages send asynchronous requests, and the Web pages modify their normal lifecycle to process callback. There are some differences between the two.
Problems with aspnet sending and callback
For example, you can see:
I want to display a news list on the page, which is displayed using the gridview control and has many pages. We generally write a showlist () method in the cs file, call showlist () in Page_Load ().
If not! IsPostBck:
When you open this page for the first time, everything is normal and the gridview is displayed normally. However, when you click the next page, the problem occurs: During Page_Load, the program will execute the showlist () method again !!!! The paging effect is invisible !!!!
Solution: add if (! IsPostBck.
If the page is opened for the first time, the showlist () method is executed. If the server space is returned, the showlist () method is no longer executed.
Understand ???
Implement client callback without sending back on the ASPNET webpage
Textbook. You are dead.