Click here to Skip to main content
15,895,656 members
Articles / Web Development / ASP.NET

Partial Rendering Control using JQuery

Rate me:
Please Sign up or sign in to vote.
4.78/5 (8 votes)
5 Aug 2010CPOL8 min read 63K   734   45  
This article shows a web custom control that allows partial rendering using JQuery
using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for Class1
/// </summary>
public class RenderFactory
{
    
    public static string RenderUserControl(Control crtl)
    {
        Control control = null;
        const string STR_BeginRenderControlBlock = "<!-- BLOCK RENDER CONTROL -->";
        const string STR_EndRenderControlBlock = "<!-- END RENDERCONTROL -->";
        StringWriter tw = new StringWriter();
        Page page = new Page();
        page.EnableViewState = false;
        HtmlForm form = new HtmlForm(); 
        form.ID = "__temporanyForm"; 
        page.Controls.Add(form);
        form.Controls.Add(new LiteralControl(STR_BeginRenderControlBlock));
        form.Controls.Add(crtl); 
        form.Controls.Add(new LiteralControl(STR_EndRenderControlBlock)); 
        HttpContext.Current.Server.Execute(page, tw, true); 
        string Html = tw.ToString();   
        //TO DO:clean the response!!!!!
        int start = Html.IndexOf("<!-- BLOCK RENDER CONTROL -->");
        int end = Html.Length - start;
        Html = Html.Substring(start,end);
        return Html;
        
    }

    public static string RenderUserControl(string crtlPath)
    {

        Control control = null;
        const string STR_BeginRenderControlBlock = "<!-- BLOCK RENDER CONTROL -->";
        const string STR_EndRenderControlBlock = "<!-- END RENDERCONTROL -->";
        StringWriter tw = new StringWriter();
        Page page = new Page();
        page.EnableViewState = false;
        control=page.LoadControl(crtlPath);
        HtmlForm form = new HtmlForm();
        form.ID = "__temporanyForm";
        page.Controls.Add(form);
        form.Controls.Add(new LiteralControl(STR_BeginRenderControlBlock));
        form.Controls.Add(control);
        form.Controls.Add(new LiteralControl(STR_EndRenderControlBlock));
        HttpContext.Current.Server.Execute(page, tw, true);
        string Html = tw.ToString();
        //TO DO:clean the response!!!!!
        return Html;

    }



}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect PeluSoft Limited
United Kingdom United Kingdom
I have been fascinated by software development since I was 10 years old. I'm proud of learned the first programming language with an Olivetti PC 128S based on Microsoft BASIC. I'm a Software Architect/Senior Developer with a strong background of all the developing Microsoft's technologies.

Comments and Discussions