Accessing HTML source code of a page using ASP.NET and Javascript

Posted by: Suprotim Agarwal , on 9/6/2007, in Category ASP.NET
Views: 104624
Abstract: In this article, we will see how to access the HTML source code of the posted page using ASP.NET and javascript. This tip uses javascript to achieve the task.
Accessing HTML Source code using ASP.NET and Javascript
 
In this article, we will see how to view the HTML source of the posted page.
To do so, follow these steps :
Step 1: Create a new ASP.NET website and rename it to “ViewHtmlSource”. Drag and drop a button. Rename the button ID to “btnViewHtml” and set its Text property to “View Html”. Similarly drag and drop a textbox and rename the textbox to “txtHtmlSrc”. Set its ‘TextMode’ to Multiline.
Step 2: In the Form Load() event, add the following code:
protected void Page_Load(object sender, EventArgs e)
{
    btnViewHtml.Attributes.Add("onClick", "javascript:getHtml()");
}
Step 3: Switch to the source tab of the Default.aspx page and add the following code in the <Head> section of the page.
<head runat="server">
    <title>View HTML</title>
    <script type="text/javascript" language="javascript">
    function getHtml(txtbox)
    {
        var src = document.documentElement.innerHTML;
        document.forms[0]['txtHtmlSrc'].value = src;
    }
    </script>
 
</head>
 
Step 4: In the <%Page> directive, add ValidateRequest=false.
 
<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
That’s it.
 
The entire source code will look like this
 
Default.aspx
 
<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest="false" 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 runat="server">
    <title>View HTML</title>
    <script type="text/javascript" language="javascript">
    function getHtml(txtbox)
    {
        var src = document.documentElement.innerHTML;
        document.forms[0]['txtHtmlSrc'].value = src;
    }
    </script>
 
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnViewHtml" runat="server" Text="View Html" />
        <br />
        <br />
        <asp:TextBox ID="txtHtmlSrc" runat="server" Height="235px" TextMode="MultiLine" Width="542px"></asp:TextBox></div>
    </form>
</body>
</html>
 
Default.aspx.cs
 
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;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnViewHtml.Attributes.Add("onClick", "javascript:getHtml()");
    }
}

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce the The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on .NET Standard and the upcoming C# 8.0 too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for ten consecutive times. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Amal on Sunday, September 9, 2007 1:56 AM
Educative article
Comment posted by Arjang on Tuesday, September 25, 2007 9:54 AM
cool...it works perfectly. thanks!
Comment posted by Bill on Wednesday, October 3, 2007 11:19 AM
This is NOT using ASP.net to do anything.  It's using javascript.  You're NOT using ASP.net to access HTML source code as your title suggests.
Comment posted by Suprotim Agarwal on Thursday, October 4, 2007 9:54 PM
Bill,
Thanks for the comment.I partially agree with you but please do read the abstract which explains this very clearly. This article is intended to call javascript from ASP.NET to access the source. This was specifically written for a user who wanted to know how to call javascript using asp.net on a button click.I just added some spice to his request :)
Comment posted by Arjang on Monday, October 15, 2007 9:36 AM
It doesn't work if ValidateRequest="true". In most of the situations, we cannot turn it off.  Just wondering is there any other way to get source code on the server?
Comment posted by Suprotim Agarwal on Saturday, October 20, 2007 6:08 AM
Dear Arjang,
You cannot get the source code on the server. As this article demonstrates, you can get the html code(the response that is sent from the server) that gets rendered at the browser.
Comment posted by Arjang on Tuesday, October 23, 2007 2:02 PM
you can get the HTML source on the server...look at the following link:
http://west-wind.com/weblog/posts/481.aspx

and turning off ValidateRequest is not recommended at all!
Comment posted by Suprotim Agarwal on Friday, October 26, 2007 9:43 AM
Hi Arjang,
Thanks for pointing me towards that link. Let me ask you, using that can you access the source code on the server of any application? Only the owner of the site can do that, right!! However this article has been created to demo that you can access the HTML source code of any page.
And yes,  turning off ValidateRequest is not recommended. It is a small hack to get over.
Comment posted by Manish T on Tuesday, April 21, 2009 1:19 AM
Hey this is good, but I want to pass the URL and get html source code of that URL.I want to use innerHTML and all.. Any idea ??
Comment posted by indian guy on Monday, April 27, 2009 6:49 AM
nice article from you buddy
http://planetsourcecode.in
Comment posted by kk.kjk on Monday, July 13, 2009 8:34 AM
very poor
Comment posted by amin on Thursday, October 1, 2009 4:51 AM
hello
thank you for that nice article.
i have a question about this:
in this article we can read HTML source of the local page that we run it.
please help me,how can i read HTML source of an external page???for example see www.yahoo.com, home page HTML source.
Comment posted by amin on Thursday, October 1, 2009 7:21 AM
hello
thank you for that nice article.
i have a question about this:
in this article we can read HTML source of the local page that we run it.
please help me,how can i read HTML source of an external page???for example see www.yahoo.com, home page HTML source.
Comment posted by Tarique on Sunday, June 20, 2010 11:03 AM
Thanks a lot i was searching this code from last two months...thanx a lot
By using this code i done it in javascript only no need to use .net or asp..
Comment posted by hiren s mistry on Wednesday, April 13, 2011 3:38 AM
tage
Comment posted by ali on Wednesday, September 7, 2011 9:47 AM
how to use html page in visual basic
Comment posted by Qutub on Tuesday, September 18, 2012 9:00 AM
As per the requirement Suprotim solution works fine.

But, I totally agree view Arjangs comment. One should not compromise security by using ValidateRequest=false.

Comment posted by Suprotim on Friday, September 21, 2012 9:45 AM
Qutub: Agreed it is a hack and thus not recommended unless absolutely needed
Comment posted by fggh on Wednesday, July 24, 2013 7:21 AM
hgfhfh
Comment posted by dfsdf on Wednesday, March 12, 2014 4:28 AM
fdvdcxvxcvxcvvxccv
Comment posted by Ganesh Taware on Wednesday, March 12, 2014 4:30 AM
i want  to call html page  (out put ) not only source code . it's not usable

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

C# .NET BOOK

C# Book for Building Concepts and Interviews

Tags

JQUERY COOKBOOK

jQuery CookBook