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:
- 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.
- Change the calendar to Ajax loading, and click "Last month", "next month" when Ajax updates the calendar content.
- 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