65.9K
CodeProject is changing. Read more.
Home

How to get the HTML source code from an editable Iframe, on the server.

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.44/5 (13 votes)

Mar 26, 2003

1 min read

viewsIcon

188356

Getting the HTML source code from an editable Iframe, on the server

Introduction

There are some times when you want to obtain the HTML source code from an editable Iframe, on the server side of the world, and this article gives a very simple example of that.

The ASP.NET form

<%@ Page Language="vb" AutoEventWireup="false" 
                Codebehind="test.aspx.vb" Inherits="test"%>
<form id=frmMain method=post runat="server">
    <iframe id=ifrHTML name=ifrHTML runat="server"></iframe>
    <asp:Button id=cmdSend runat="server" Text="Send"></asp:Button>
    <input type=hidden name=hidValue>
</form>
<script>
    //Set the IFRame to Desing Mode.
    ifrHTML.document.designMode = "on";            
</script>

As you can see from the code above we have the Iframe, a submit button, and a hidden field.

We will need the hidden field to store the value of the innerHTML value of the Iframe in the client side, so we can submit later the hidden field into the server side code.

CodeBehind

Private Sub Page_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
    cmdSubmit.Attributes.Add("onClick", _
       "document.frmMain.hidValue.value = ifrHTML.document.body.innerHTML;")
End Sub

Private Sub cmdSubmit_Click(ByVal sender As System.Object,_
     ByVal e As System.EventArgs) Handles cmdSubmit.Click
    Dim strValue As String

    strValue = Request.Form("hidValue")
End Sub

First we'll take a look at the Page_Load function. As you can see there's just a simple attribute addition to the cmdSubmit button. We added the onClick event, so whenever the user clicks this button, the value of the HTML source within the editable Iframe, will be stored on the hidden field hidValue.

Now the cmdSubmit_Click event. Here's where we actually get the HTML source that the Iframe passed to the hidden field on the client side, and we get it by a simple Request.Form of the hidden field, and store it into the string variable strValue.

That's it, you now have the HTML source code of an editable Iframe into a string variable on the server.

Conclusion

There are many ways of storing the HTML source code of an Iframe into the server side, this is just one of them. I hope you find it useful.