Problems with overriding the Render method

I want to serialize all my web form output (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()));


but I am getting this error:

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 going wrong? Does it do it right?


source to share

1 answer

I don't think you have internal

when overriding

protected override void Render(HtmlTextWriter writer)


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

An override declaration cannot change the accessibility of a virtual method. However, if the protected base method is protected internally, and is declared in a different assembly than the assembly containing the override method, then the declared override methods must be protected.

Link here

Perhaps something like this:

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

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

    //Now output it to the page, if you want



We know that all pages inherit from page

. We also know what new htmltextwriter

takes in stringwriter

, which has stringbuilder

in contructor. When we then call the base class ( page

) to render the html in our new htmltextwriter

. This does it too htmltextwriter

, which also displays stringbuilder

. So now we have html context in our stringbuilder

. Then we will just tell the input htmltextwriter

to write string

from ours stringbuilder


Link here



All Articles