|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionWhen you need to dynamically control the HTML that is generated from your ASP.Net page or control, a famous technique to do that is to override the render function protected override void Render(HtmlTextWriter writer)
{
writer.Write("SayAnthing");
}
What is the ProblemNow let us change page data instead of replacing it. The attached sample contains two aspx pages, both have the same HTML structure as follows: <table> <tr> <td>
Hello ( <%Response.Write(@"Mr. XYZ"); %> $MSG$
</td> </tr> </table>
All we need to do is to replace $MSG$ with a message "How R U?" by capturing the rendering event. The required result should be as in the following figure:Let us write down the code: protected override void Render(HtmlTextWriter writer)
{
// Create your HTMLTestWriter
System.Text.StringBuilder oPageContents = new System.Text.StringBuilder();
System.IO.StringWriter oSW = new System.IO.StringWriter(oPageContents);
System.Web.UI.HtmlTextWriter oHtmlWriter = new System.Web.UI.HtmlTextWriter(oSW);
// Give the page a fake streamer to capture HTML contents.
base.Render(oHtmlWriter);
oPageContents = oPageContents.Replace("$MSG$", "How R U ?");
// Write data back into the original stream
System.IO.StringReader oSR = new System.IO.StringReader(oPageContents.ToString ());
writer.Write(oSR.ReadToEnd ());
}
Nothing strange about this code and it can be found all over the internet, the idea behind it is that you replace the given Solution - HTML StreamerHTMLStreamer solves this issue by acting as a double streamer, it streams data to StringBuilder object as in our previous sample, in the same time, it streams data to writer parameter of type HtmlTextWriter. The data is submitted synchornized to Response object, while another copy is being saved in the string builder object. public class HTMLStreamer: System.IO.StringWriter
{
protected System.Web.UI.HtmlTextWriter mHtmlTextWriter;
public HTMLStreamer(System.Text.StringBuilder oStringBuilder, System.Web.UI.HtmlTextWriter innerWriter)
: base(oStringBuilder)
{
mHtmlTextWriter = innerWriter;
}
public override void Write(string value)
{
// Change content dynamically here.
if (value.IndexOf("$MSG$")!=-1)
{
value = value.Replace("$MSG$","How R U ?");
}
mHtmlTextWriter.Write(value); base.Write(value);
}
public override void Write(char[] buffer, int index, int count)
{
mHtmlTextWriter.Write(buffer, index, count);
base.Write(buffer, index, count);
}
public override void Write(char value)
{
mHtmlTextWriter.Write(value);
base.Write(value);
}
}
Now back to the protected override void Render(HtmlTextWriter writer)
{
// Create your string builder.
System.Text.StringBuilder oPageContents = new System.Text.StringBuilder();
// Create HTMLStreamer and put both writer & oPageContents into it.
HTMLStreamer oSW = new HTMLStreamer(oPageContents, writer);
// Create HTMLTextWriter that contains the double-stream class "HTMLStreamer"
System.Web.UI.HtmlTextWriter oHtmlWriter = new System.Web.UI.HtmlTextWriter(oSW);
// Get HTML contents.
base.Render(oHtmlWriter);
}
EnhancementsA delegate is called in all Write() functions of HTMLStreamer so that it can be used by the page to change contents without the need to write into HTMLStreamer itself, so it can be used as a separate DLL. For sure if you have any idea how to capture
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||