Accessing HTML source code of a page using ASP.NET and Javascript
Posted by: Suprotim Agarwal ,
on 9/6/2007,
in
Category ASP.NET
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.
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 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 Book 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 the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) 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!
Was this article worth reading? Share it with fellow developers too. Thanks!
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