Some way to modify rendered output html


Another way to use a global variable in your page as overriding render method of your form. Ok its not a good and suggested way and also have performance problems but strangely it will work sometimes and also provide writing less code in html side.

Our form’s html view:

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”WebForm1.aspx.cs” Inherits=”MultiLangTest.WebForm1″ %>

<!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 runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>

$$GlobalVariable

<a href=”http://www.google.com” >$$GlobalVariable</a>

<div title=”$$GlobalVariable” > </div>
<div title=”$$GlobalVariable” > </div>
<div title=”$$GlobalVariable” > </div>
<div title=”$$GlobalVariable” > </div>
<div title=”$$GlobalVariable” > </div>

$$GlobalVariable

<!–
<asp:Label ID = “Label1″  runat=”server”
Text =”<%= You know Its not working with asp:x tags
and also not necessery but sometimes strangely
you want to use this %>” />

than You can do it like this:
–>
<asp:Label ID = “Hello”  runat=”server” Text =”$$GlobalVariable” />

</div>
</form>
</body>
</html>

Our form’s codebehind:

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void Render(HtmlTextWriter writer)
        {
            //we will create a memory stream for holding data
            using (MemoryStream ms = new MemoryStream())
            {
                //A streamwriter to write memory
                using (StreamWriter sw = new StreamWriter(ms))
                {
                    //this is our html writer.
                    HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
                    //Complete other renders
                    base.Render(htmlWriter);

                    htmlWriter.Flush();

                    //Set memory stream to start position.
                    ms.Position = 0;

                    //read the data
                    using (System.IO.StreamReader _Reader = new System.IO.StreamReader(ms))
                    {
                        string txt = _Reader.ReadToEnd();
                        //replace global variable
                        txt = txt.Replace("$$GlobalVariable", "Hello World" );
                        Response.Write(txt);
                        _Reader.Close();
                    }
                }
            }
        }
    }

Here is the output:

<!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><title>

</title></head>
<body>
<form name=”form1″ method=”post” action=”WebForm1.aspx” id=”form1″>
<div>
<input type=”hidden” name=”__VIEWSTATE” id=”__VIEWSTATE” value=”/wEPDwUKMTg3NjI4NzkzNmRkxS9N0UwBHiMGrNo9vYAKQmdAfAc=” />
</div>

<div>

Hello World

<a href=”http://www.google.com” >Hello World</a>

<div title=”Hello World” > </div>
<div title=”Hello World” > </div>
<div title=”Hello World” > </div>
<div title=”Hello World” > </div>
<div title=”Hello World” > </div>

Hello World

<!–
<span id=”Label1″><%= You know Its not working with asp:x tage
and also not necessery but sometimes strangely
you want to use this %></span>

than You can do it like this:
–>
<span id=”Hello”>Hello World</span>

</div>
</form>
</body>
</html>

Advertisement

About bpostaci
Escalation Engineer in Microsoft.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: