有关重写Render方法的问题 [英] Problems on override the Render method

查看:120
本文介绍了有关重写Render方法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的Web窗体的所有输出(从.NET 3.5上的aspx和aspx.cs)序列化为JSON.所以,这是我的代码:

I want to serialize all my output of a Web Form (from aspx and aspx.cs, on .NET 3.5) to JSON. So, this is my code :

protected string myText;

protected void Page_Load(object sender, EventArgs e)
{
    myText = "<div><span>This is my whole code</span><div><a style=\"color:blue !important;\" href=\"#\">A link</a></div></div>";
}

protected internal override void Render(HtmlTextWriter writer)
{
    var serializer = new JavaScriptSerializer();
    Response.Write(Request["callback"] + serializer.Serialize(writer.ToString()));
}

但是我得到这个错误:

CS0507: 'moduli_Prova.Render(System.Web.UI.HtmlTextWriter)': cannot change access modifiers when overriding 'protected' inherited member 'System.Web.UI.Control.Render(System.Web.UI.HtmlTextWriter)'

我在哪里错了?这是正确的方法吗?

Where am I wrong? Is this the right method to doing it?

推荐答案

我不认为您在覆盖上有internal

I don't think you have internal on a override

protected override void Render(HtmlTextWriter writer)

覆盖虚拟方法时,我们无法修改访问修饰符 在派生类中.

We cannot modify the access modifiers when overriding a virtual method in derived class.

覆盖声明不能更改虚拟机的可访问性 方法.但是,如果覆盖的基本方法是内部受保护的, 并且在与程序集不同的程序集中声明 包含替代方法,然后声明替代方法 可访问性必须受到保护.

an override declaration cannot change the accessibility of the virtual method. However, if the overridden base method is protected internal and it is declared in a different assembly than the assembly containing the override method then the override method’s declared accessibility must be protected.

参考此处

也许是这样的:

protected override void Render (HtmlTextWriter writer)
{
    StringBuilder sb = new StringBuilder();
    HtmlTextWriter tw = new HtmlTextWriter(new System.IO.StringWriter(sb));
    //Render the page to the new HtmlTextWriter which actually writes to the stringbuilder
    base.Render(tw);

    //Get the rendered content
    string sContent = sb.ToString();

    //Now output it to the page, if you want
    writer.Write(sContent);
}

修改

我们知道所有页面都继承自page..我们还知道,新的htmltextwriter接受了在构造器中具有stringbuilderstringwriter.然后,当我们调用基类(page)将html呈现为新的HtmlTextWriter时.它也渲染htmltextwriter,也渲染为stringbuilder.现在,我们在stringbuilder中有了html上下文.然后,我们只是对输入的HtmlTextWriter说,应该从我们的stringbuilder中写出string.

We know that all pages inherit from page.. We also know that a new htmltextwriter take in a stringwriter that has a stringbuilder in the contructor. When we then call the base class (page) to render the html to our new HtmlTextWriter. It render it too the htmltextwriter that also renders to the stringbuilder. So now we have the html context in our stringbuilder. Then we just say to the inputed HtmlTextWriter that is should write the string from our stringbuilder.

参考 查看全文

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