Load WebForms user control (. ascx) in ASP.

Source: Internet
Author: User

Original: WebForms user control (. ascx) loaded in ASP.

Issue background

The calendar in the blog Park blog uses the ASP. NET WebForms Calendar control (System.Web.UI.WebControls.Calendar), which will be the "last month", "next month" link to "__doPostBack ()" JS Call, Such as:

It now finds two questions:

1. IE10 is not supported;

2. Some computers do not allow the execution of __doPostBack.

Problem extraction

Premise:

    1. We want to solve this problem at the lowest cost, which is to make as few changes to the current code as possible. So try to reuse the existing Calendar control code as much as possible.
    2. Change the calendar to Ajax loading, and click "Last month", "next month" when Ajax updates the calendar content.
    3. Use ASP. NET MVC to process AJAX requests.

To resolve the issue:

How to load the user control (. ascx) that contains the WebForms calendar control and get its output string in the ASP. NET MVC controller, and then replace the __doPostBack code with the Ajax calling code.

Core issues:

How to get the string from the user control (. ascx) output in the ASP.

Workaround

Look at the code first

 PublicActionResult Calendar () {varpage =NewPage (); varform =NewHtmlForm (); varCalendar = page. LoadControl ("~/controls/cnblogscalendar.ascx"); Form.    Controls.Add (Calendar); Page.    Controls.Add (form); using(varSW =NewStringWriter ()) {System.Web.HttpContext.Current.Server.Execute (page, SW,true); returnContent (SW.    ToString ()); }}

The code was simple, but getting this code took time this morning.

Code Description:

    • Must be New page (), only Page can loadcontrol.
    • You must have new HtmlForm (), because the calendar control needs to be placed between <form runat= "Server" >.
    • The key contributor is HttpContext.Current.Server.Execute, which dynamically loads the control and outputs the string entirely by it. The hero was found in this article (thanks to Sam Mueller). The way I used it before (the usual way to go: Using Web Forms User controls in ASP.) is not only cumbersome, but also problematic in this scenario.

Code Run Result:

Full code Download:

Http://files.cnblogs.com/dudu/CNBlogsDemoMvcAscx.rar

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.