3

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?

markzzz
  • 47,390
  • 120
  • 299
  • 507

1 Answers1

10

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

Community
  • 1
  • 1
Arion
  • 31,011
  • 10
  • 70
  • 88
  • Right! But unfortunatly this print `request?"System.Web.UI.HtmlTextWriter"`, not the output of web form converted to JSON :O – markzzz Mar 16 '12 at 10:54
  • 1
    @markzzz, because you're writing the object itself, not it's output. – Moo-Juice Mar 16 '12 at 10:56
  • uhm? So, how can I get the output? from that object I suppose? I don't see any method that doing it... – markzzz Mar 16 '12 at 10:57
  • Can you help me? How can I access to the output? – markzzz Mar 16 '12 at 11:32
  • Not sure about you have done it...but it ROCKS! Thank you...master! P.S. Can you explain in details how you have done it? And how it works? Would be a nice document, also for the future... – markzzz Mar 16 '12 at 15:09
  • Update the answer. I hope it help you understand – Arion Mar 17 '12 at 15:48