How to change some text before it is sent to the client in asp.net with example

Category > ASP.NET || Published on : Monday, December 14, 2015 || Views: 9808 || change some text before it is sent to the client change text before rendring


Introduction

Here Pawan Kumar will explain how to how to modify the response before it is sent to the client.The trick here is to override the Render method of the Page class and modify the response.

Description

In previous post I have explained How to replace number from a string in C#, How to Download Json Data from URL in Asp.Net using C sharp, How to create draw a doughnut chart using ChartJS in Asp.net detailed Example, Code Snippet for Deleting a SQL Server stored procedure, Bootstrap style DropDownList example in ASP.Net, Set DropDownList value based on text/value in jQuery, and many more articles.

Now I will explain How to How to change some text before it is sent to the client in asp.net with example

So follow the steps to learn How to change some text before it is sent to the client in asp.net with example

Step 1: First, We start by creating an Empty asp.net web site in Visual Studio .NET 2013.

Step 2: Add a new web page name "Default.aspx" with the following codes:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>change some text before it is sent to the client</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Hello World!
        <br />
        Hi
    </div>
    </form>
</body>
</html>

Step 3: Add the following codes in Code Behind file:-

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Filter = new ReplaceHTML(Response.Filter);
    }

    /// <summary>
   /// Second Method!!!!!!! overide the Render Method.
   /// </summary>
   /// <param name="writer"></param>
   protected override void Render(HtmlTextWriter writer)
   {


       StringWriter output = new StringWriter();
       base.Render(new HtmlTextWriter(output));
       writer.Write(output.ToString().Replace("Hi", "Before rendering it was  'Hi', and after rending it is 'God Morning'"));
   }
}

Step 4: Now Create a Class file with name "ReplaceHTML.cs" to the app_code folder with the following codes:-

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// To intercept and get a reference to the HTML, we now need to create a
/// class to inherit System.IO.Stream. So, create a new class in
/// </summary>
public class ReplaceHTML : System.IO.Stream
{
    private System.IO.Stream Base;

    public ReplaceHTML(System.IO.Stream ResponseStream)
    {
        if (ResponseStream == null)
            throw new ArgumentNullException("ResponseStream");
        this.Base = ResponseStream;
    }


    public override int Read(byte[] buffer, int offset, int count)
    {
        return this.Base.Read(buffer, offset, count);
    }


    public override void SetLength(long value)
    {

    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        // Get HTML code 
        string HTML = System.Text.Encoding.UTF8.GetString(buffer, offset, count);

        // Replace the text with something else 
        HTML = HTML.Replace("Hello World!", "I've replaced the Hello World example!");

        // Send output 
        buffer = System.Text.Encoding.UTF8.GetBytes(HTML);
        this.Base.Write(buffer, 0, buffer.Length);
    }

    public override bool CanRead
    {
        get { throw new Exception("The method or operation is not implemented."); }
    }

    public override bool CanSeek
    {
        get { throw new Exception("The method or operation is not implemented."); }
    }

    public override bool CanWrite
    {
        get { throw new Exception("The method or operation is not implemented."); }
    }

    public override void Flush()
    {
        HttpContext.Current.Response.Flush();

    }

    public override long Length
    {
        get { throw new Exception("The method or operation is not implemented."); }
    }

    public override long Position
    {
        get
        {
            throw new Exception("The method or operation is not implemented.");
        }
        set
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }

    public override long Seek(long offset, System.IO.SeekOrigin origin)
    {
        throw new Exception("The method or operation is not implemented.");
    }
}

Output of the How to change some text before it is sent to the client in asp.net with example

Conclusion:

So, In this tutorial we have learned, How to change some text before it is sent to the client in asp.net with example

Download Source Codes