[Original] dynamic parsing of user controls in ASP. net mvc Controllers

Source: Internet
Author: User

Currently, the project requires page adjustment by module.. net mvc user control is implemented to split the page into multiple modules. Each module is a user control. You can drag and drop the module to adjust the page layout and save it as configuration information, then the Controller reads the personalized configuration of each user and loads and parses the control in the action, and then presents it to the view. The following is the code for dynamically parsing the user control:

Public ActionResult Design (string shopid)
{
Var page = new System. Web. Mvc. ViewPage (); // create an MVC Page Object
// Create a custom control that is of the MVC type
ViewUserControl calendar = (ViewUserControl) page. LoadControl ("~ /Components/Design. ascx ");
StringWriter output = new StringWriter ();
ViewData ["data"] = "data ";

Using (var sw = new System. Web. UI. HtmlTextWriter (output ))
{
Page. Controls. Add (calendar); // Add a user control to the page. Independent Controls cannot be parsed.
Calendar. ViewData ["data"] = "data"; // value ViewData
Page. RenderControl (sw); // parse the control
// System. Web. HttpContext. Current. Server. Execute (page, sw, true );
ViewData ["control"] = sw. InnerWriter. ToString (); // output parsed content
}
Output. Dispose ();


Return View ();

}

Control Code:

<%@ Control Language="<span class="wp_keywordlink_affiliate"><a href="http://www.tangdou.net/tag/c/" title="C#" target="_blank">C#</a></span>" AutoEventWireup="true" CodeBehind="Design.ascx.cs" Inherits="NewTang.Components.Design" %><div>design:<%=ViewData["data"] %></div>

My blog: www.mikel.cn

Shundao recruitment program ape and siege lion engaged in ASP. net mvc development

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.