Ascx + wrapper page + jquery Ajax Solution

Source: Internet
Author: User

First, understand what is wrapper page.
Wrapper page is a kind of webform specifically used to wrap the user control ascx. The render method of the wrapper page is overwritten, and only the usercontrol Member of the wrapper page is render. In this way, we can use ascx to output clean HTML fragments. Yes, our goal is to output clean HTML clips!

--------------------------------------------------------------------------------

Implementation Method
The goal is clear and the idea is ready. Now let's implement it ~
Simple: Write a wrapper Page Base, which is used as the base class of all wrapper pages. Later wrapper pages will inherit from it. This class (wrapperpagebasel class) inherits from the system. Web. UI. Page class. It only needs to do two things:

Has a usercontrol member.
Override the render method of the parent class (page)
Then we can execute the render method of usercontrol In the rewrite render method. SeeCode1 ):

///


// Ajax packaging page base class
///
public class wrapperpagebase: system. web. UI. page
{< br> Public usercontrol {Get; Set ;}< br>
protected override void render (htmltextwriter writer)
{< br> If (control = NULL)
control = (usercontrol) This. loadcontrol ("~ /Controls/dummy. ascx "); // a dummy. If it is null, server error is output ..
control. rendercontrol (writer);
}< BR >}wrapper page inherits from the wrapperpagebase class:

Public partial class ajax_test: wrapperpagebase
{
Protected void page_load (Object sender, eventargs E)
{
This. control = (usercontrol) This. loadcontrol ("~ /Controls/any. ascx "); // useful User Controls
}
} Use jquery asynchronous call on the page. See the example of ashx + jquery, a lightly Ajax solution of Asp.net.
Your page only needs one sentence:

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "test. aspx. cs" inherits = "ajax_test" %> you can delete all other items. -- Of course, you can also choose not to delete the code, and you will find that the Code will not appear on the page. You can also switch usercontrol in the same wrapper page, as long as you determine the query string, as shown in the following code:

Protected void page_load (Object sender, eventargs E)
{
VaR STR = request ["want"];
If (STR = "iwanta ")
This. control = (usercontrol) This. loadcontrol ("~ /Controls/a. ascx "); // user control
Else if (STR = "iwantb ")
This. control = (usercontrol) This. loadcontrol ("~ /Controls/B. ascx "); // user control
Else
This. control = (usercontrol) This. loadcontrol ("~ /Controls/whatdoyouwant. ascx "); // user control whatdoyouwant
} Now, this solution is complete.

 

--------------------------------------------------------------------------------

Why?
In fact, we don't need wrapperpagebase, we don't need to rewrite the render method, simply delete all the HTML code in webform, and then replace it with the code we put in ascx, in this way, HTML fragments can be output. But why is it so troublesome ????
The reasons are as follows:

you do not need to delete the code in webform
you do not need to modify it. The existing ascx
mechanism is transparent. You only need loadcontrol.
finally, you can output different HTML fragments based on different query strings in the same webfrom, even if these fragments are irrelevant! Amazing!
1) a dummy is specified in the Code. when the content of ascx is empty or dummy is not specified, htmltextwriter will display an HTML page with no content but complete content, which is not the HTML part we want

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.