Today, we encountered such a problem: click a row of a gridview control and show detailed data of this row in another gridview. This is simple, use the intermediate table to obtain the data source and bind it to the new gridview. However, every time the page is refreshed, there is no smooth and continuous user experience. Therefore, use updatepanel.
Before the instance, let's talk about updatepanel. The common attributes are as follows:
Role of attributes
- Rendrmode: Content appearance mode. inline is the row mode, and block is the block model.
- Updatemode: Content update mode. Always refresh as long as there is interaction, and conditional is updated only when the specified condition interaction.
- Childrenastrigger: determines whether subinteraction is used as a trigger. The default value is true, indicating that subinteraction is used as a trigger.
- Trrigers: trigger. contorlid indicates the trigger and refresh, and eventname indicates the event of the Control. asyncpostbacktrigger indicates asynchronous refresh and only updatepanel content is refreshed. If it is postbacktrigger, it indicates synchronous refresh, page Refresh: Changes to normal refresh, that is, reload the entire page.
- Contenttemplate: content of the region to be refreshed.
Notes
- Updatepanel can be nested, but childrentrigger refers to direct subinteraction. Nesting is not allowed.
- The conflict between updatemode and childrenastrigger when updatemode is always.
To use ASP. NET Ajax, you must have scriptmananger before updatepanel, as shown below:
<asp:ScriptManager ID="ScriptManager1" runat="server" ></asp:ScriptManager><asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" RenderMode ="Block" UpdateMode="Conditional" > <ContentTemplate> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger /> <asp:PostBackTrigger /> </Triggers></asp:UpdatePanel>
Basic Principles
Open the web page source code (chrome) and you can see:
<script type="text/javascript"> //<![CDATA[Sys.WebForms.PageRequestManager._initialize('ctl00$ScriptManager1', 'aspnetForm', ['tctl00$ContentPlaceHolder1$upChildrenAsTriggers','','fctl00$ContentPlaceHolder1$upNotChildrenAsTriggers',''], ['ctl00$ContentPlaceHolder1$btnOutsideAsyncPostbackButton',''], ['ctl00$ContentPlaceHolder1$btnFullPostback',''], 90, 'ctl00');//]]></script>
- Output the required content to the client and receive four parameters: array 1, array 2, array 3, and number.
- The first array is updatecontrols: The updatepanel control of the server is an element on the client: Change $ to _. The content of the updatepanel control is the ID of the client control. The client ID is separated by an underscore, and the server ID is separated by $. The server ID is also output to the client. The second array is the Control ID to be asynchronously refreshed, which is forced to be asynchronously refreshed. The third array is the complete refresh control ID. The fourth parameter indicates the specified Timeout time.
- Chrome browser -- Developer Tools --- source --- drop-down menu --- sources --- localhost --- updatepanelclientside --- scriptresource. axd, used to intercept the client's submission method: $ _ onformelementclick (when you click it, apply for additional information for asynchronous refresh, and generate the postbacksettings information. Based on the submitted information, determine whether the request is asynchronous refresh), $ _ onformsubmit (maintain the ASPNET page function, ensure that asynchronous refresh is the same as synchronous refresh, execute the necessary methods, then construct the formbody information, and finally construct the request), $ _ dopostback (click the link and then enter this method. If it is an asynchronous commit, it will re-enter onforsubment ).
- If it is determined to be synchronous or asynchronous, check whether its parent element is updatepanel Based on the DOM element. The elements placed in usercontrol are also determined based on the DOM method; and query the server ID through clientid.
- Updatepanel depends on the scriptmanager Server Control and the client pagerequestmanager. When the scriptmanager allows partial page updates, the updatepanel is asynchronously transmitted to the server, unlike the traditional full-page callback, only the part of updatepanel is refreshed. After HTML is returned from the server, pagerequestmanager updates the content by operating on the DOM object.
Simple instance
As mentioned at the beginning of the article, the Code is as follows:
<H4> package Introduction: </H4> <br/> <% -- scriptmanager -- %> <asp: scriptmanager id = "scriptmanager1" runat = "server"> </ASP: scriptmanager> <% -- show all plans overview -- %> <asp: gridview id = "gvmeal" runat = "server" Height = "173px" width = "656px" onselectedindexchanging = "gvmeal_selectedindexchanging"> <columns> <asp: commandfield headertext = "View Details" showselectbutton = "true"/> </columns> </ASP: gridview> <br/> <H4> package details: </H4> <% -- View details of a plan -- %> <% -- trigger with sub-content and conditional -- %> <asp: updatepanel id = "updatepanel1" runat = "server" childrenastriggers = "true" updatemode = "Conditional"> <contenttemplate> <% -- The gridview to be updated -- %> <asp: gridview id = "gvdetails" runat = "server" Height = "54px" width = "648px"> </ASP: gridview> </contenttemplate> <triggers> <% -- asynchronous update -- %> <% -- the trigger is gvmeal, and the trigger event is selectedindexchanging -- %> <asp: asyncpostbacktrigger controlid = "gvmeal" eventname = "selectedindexchanging"/> </triggers> </ASP: updatepanel>
Background code
Protected void page_load (Object sender, eventargs e) {If (! Ispostback) {// bind the package overview information gvmeal. datasource = getdataset (). tables [0]; gvmeal. databind () ;}}// dataset public dataset getdataset () {dataset mealandsection = new dataset (); mealandsection = frontmanager. queryallmealandsections (); Return mealandsection;} protected void gvmeal_selectedindexchanging (Object sender, gridviewselecteventargs e) {// bind gvdetails to the details of the package based on the selected package. datasource = getdataset (). tables [E. newselectedindex + 1]; gvdetails. databind ();}
ASP. net Ajax, you can see that ASP. net Ajax is easy to use without writing JS Code. Microsoft has always been like this: it encapsulates complicated content and makes it easier for programmers to use it. Microsoft is doing well in simple use, but from the depth of the programmer's knowledge, it is not good: like building a house, it has been using bricks burned by others. If you want to use Ajax well, you can still develop it from bottom up, learn JS well, and burn your own bricks.