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()));
}
but I get 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 wrong? Is this the right method to doing it?
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.
Reference here
Maybe 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
base.Render(tw);
//Get the rendered content
string sContent = sb.ToString();
//Now output it to the page, if you want
writer.Write(sContent);
}
Edit
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
.
Reference here