渲染控制,无需页面 [英] Render Control Without Page

查看:97
本文介绍了渲染控制,无需页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与一个内部框架的工作。这一切都很大,但框架不使用aspx页面,它呈现使用的Response.Write页面。我遇到的问题是,我需要在页面上使用RAD的控制,因此,所有我需要做的是产生弧度控制,以获得HTML code,所以我可以转储到页面上。这可能吗?我试图创建一个Page对象得到它的工作,但它不工作。看来,当一个页面被调用时,它做一些其他的初始化。我也试过铸造HttpContext.Current.Handler到一个页面,而是因为他们使用的是其他类,而不是System.Web.UI.Page来处理请求,没有工作。

在这一点上,我也不太清楚什么尝试。如果我能MIMC页面创建类,那将是美好的。有谁知道确切的步骤正确创建和初始化一个Page对象?

希望这是有道理的......如果没有,我会尽量使其更容易理解。

谢谢!

更新

只是想给你的code,我摆弄。希望这会告诉你什么是我想要做的:

这是工作:

 的HtmlForm F =新的HtmlForm();
            RadEditor重新=新RadEditor();
            f.ID =form1的;
            this.Page.Controls.Add(F);            re.ID =rdControl;            StringBuilder的SB =新的StringBuilder();
            StringWriter的TW =新的StringWriter(某人);
            HtmlTextWriter的HW =新的HtmlTextWriter(TW);            //页P =新的页面(); //(页)HttpContext.Current.Handler;            //的HtmlForm F =新的HtmlForm();            //p.Controls.Add(f);            f.Controls.Add(重);

这是行不通的:

 的HtmlForm F =新的HtmlForm();
            RadEditor重新=新RadEditor();
            f.ID =form1的;
网页P =新的页面();            p.Controls.Add(F);            re.ID =rdControl;            StringBuilder的SB =新的StringBuilder();
            StringWriter的TW =新的StringWriter(某人);
            HtmlTextWriter的HW =新的HtmlTextWriter(TW);            //页P =新的页面(); //(页)HttpContext.Current.Handler;            //的HtmlForm F =新的HtmlForm();            //p.Controls.Add(f);            f.Controls.Add(重);

最好我想获得第二个工作在一定程度上,所以我可以生成code。很抱歉的混乱。这是我的测试code。

更新

所以,我想过这个问题,我想我知道那里的问题。控制正试图访问一堆未包含在DLL中的文件,如语言文件,样式,js文件等,我认为这个问题可能是,当我创建页面的新实例,它不知道其中,该页面是在参考的所有资源。有没有一种方法,我可以在页面设置为一个虚拟的网址是什么?现在我玩这个code:

  HttpContext的背景下=新的HttpContext(新的Htt prequest(的String.Empty,HTTP://本地主机:2090 / Default.aspx的的String.Empty),新的Htt presponse(NULL));


解决方案

在大量的研究,我设法找出我错过生成页面。显然,有一个功能,p.DesignerInitialize(),它初始化页面。一旦被调用,它生成的页面和控件可以呈现关闭它。下面是我用我的新功能:

 公共字符串RenderRad()
    {
        StringBuilder的SB =新的StringBuilder();
        StringWriter的TW =新的StringWriter(某人);
        HtmlTextWriter的HW =新的HtmlTextWriter(TW);        我的页面P =新我的页面();
        p.DesignerInitialize();        的HtmlForm F =新的HtmlForm();        p.Controls.Add(F);        f.Controls.Add(重);        re.RenderControl(HW);        返回sb.ToString();
    }

我也有一个类,从页面继承时将禁止检查所有控件是在形式:

 公共部分类我的页面:System.Web.UI.Page
{
    公共覆盖无效VerifyRenderingInServerForm(控制控制)
    {        返回;
    }
}

I am currently working with an internal framework. It's all great, but the framework doesn't use aspx pages, it renders pages using Response.Write. The issue I am having is that I need to use a rad control on the page, so all I need to do is generate the rad control to get the HTML code so I can dump it onto the page. Is this possible? I've tried creating a Page object to get it working, but it doesn't work. It seems that when a page is called, it's doing some other initializing. I also tried casting the HttpContext.Current.Handler to a page, but that didn't work because they are using another class, not System.Web.UI.Page to handle requests.

At this point I'm not too sure what else to try. If I can mimc the Page creation class, that would be wonderful. Does anyone know the exact steps to create and initialize a Page object properly?

Hopefully this makes sense... if not, I'll try to make it more understandable.

Thanks!

Update

Just wanted to give you the code that I am playing with. Hopefully this will show you what I am trying to do:

this is working:

HtmlForm f = new HtmlForm();
            RadEditor re = new RadEditor();
            f.ID = "form1";


            this.Page.Controls.Add(f);

            re.ID = "rdControl";

            StringBuilder sb = new StringBuilder();
            StringWriter tw = new StringWriter(sb);
            HtmlTextWriter hw = new HtmlTextWriter(tw);

            //Page p = new Page();//(Page)HttpContext.Current.Handler;

            //HtmlForm f = new HtmlForm();

            //p.Controls.Add(f);

            f.Controls.Add(re);

This is not working:

HtmlForm f = new HtmlForm();
            RadEditor re = new RadEditor();
            f.ID = "form1";
Page p = new Page();

            p.Controls.Add(f);

            re.ID = "rdControl";

            StringBuilder sb = new StringBuilder();
            StringWriter tw = new StringWriter(sb);
            HtmlTextWriter hw = new HtmlTextWriter(tw);

            //Page p = new Page();//(Page)HttpContext.Current.Handler;

            //HtmlForm f = new HtmlForm();

            //p.Controls.Add(f);

            f.Controls.Add(re);

ideally I'd like to get the second one working to some degree so I can generate the code. Sorry about the mess. It was my test code.

Update

So I thought about it, and I think I know where the issue is. The control is trying to access a bunch of files that are not included within the dll, like language files, styles, js files, etc. I think the issue might be that when I create a new instance of Page, it doesn't know where that page is in reference to all the resources. Is there a way i can set the page to a "virtual" URL? Right now I am playing with this code:

HttpContext context = new HttpContext(new HttpRequest(string.Empty, "http://localhost:2090/default.aspx", string.Empty), new HttpResponse(null));

解决方案

After lots of research, I managed to find out what I was missing to generate the page. Apparently there is a function, p.DesignerInitialize(), that initializes the page. Once this is called, it generates the page, and controls can be rendered off of it. Here is my new function that I am using:

public string RenderRad()
    {
        StringBuilder sb = new StringBuilder();
        StringWriter tw = new StringWriter(sb);
        HtmlTextWriter hw = new HtmlTextWriter(tw);

        MyPage p = new MyPage();
        p.DesignerInitialize();

        HtmlForm f = new HtmlForm();

        p.Controls.Add(f);

        f.Controls.Add(re);

        re.RenderControl(hw);

        return sb.ToString();
    }

I also have a class that inherits from Page that will disable the check for all controls to be in the form:

 public partial class MyPage : System.Web.UI.Page
{
    public override void VerifyRenderingInServerForm(Control control)
    {

        return;
    }
}

这篇关于渲染控制,无需页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆