Click here to Skip to main content
Click here to Skip to main content

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

By , 25 Mar 2003
 

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Hugo Flores
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: Empty String, doesn't work.memberbswiftly14 Sep '04 - 8:54 
this is what is going on..which may affect it.
 
page loads with a Session variable which is the url of the iframe. when the iframe loads it redirects itself to the location of that session variable.
 
i noticed also on your code that you have a cmdSubmit button as well as a cmdSend button. is this intended? Or do i need an html (as opposed to an asp) button as well?
GeneralRe: Empty String, doesn't work.memberHugo Flores14 Sep '04 - 9:43 
The session variable could be affecting it, but I wouldn't be sure. About the cmdSendB button, that was a typo in the aspx page should be cmdSubmit.
 
It's not why you're running, it's where your going
It's not what you're dreaming , but what you're going to do
It's not where you're born, it's where you belong
It's not how weak but what will make you strong
GeneralRe: Empty String, doesn't work.memberHugo Flores14 Sep '04 - 9:38 
I tested the code myself and indeed it works, so I don't know what's going on. I tested in Win2k Pro SP4, VS.NET 2003 .NET Framework 1.1
 
It's not why you're running, it's where your going
It's not what you're dreaming , but what you're going to do
It's not where you're born, it's where you belong
It's not how weak but what will make you strong
GeneralRe: Empty String, doesn't work.memberbswiftly14 Sep '04 - 9:50 
weird. well do you know of another way of doing this?
 
one more thought I have.. my page is set in a subframe. all of this is happenening in the lower part of the main page. so one single top frame and one lower one. the cmdExtract is in the lower, and the iframe is in the lower.
GeneralRe: Empty String, doesn't work.memberbswiftly14 Sep '04 - 14:45 
alright. I even tried your exact code in a VB version and it doesn't work.
 
So there must be some kind of system setting. Service Pack 2 would be a nice cop out but if you know of anything else it'd be a great help. It must be some kind of security setting...know of anything?
 

Thanks.
GeneralRe: Empty String, doesn't work.memberHugo Flores15 Sep '04 - 1:31 
Have you tried the same code without frames?
Service Pack 2, is that for Win XP?
 

 
It's not why you're running, it's where your going
It's not what you're dreaming , but what you're going to do
It's not where you're born, it's where you belong
It's not how weak but what will make you strong
GeneralRe: Empty String, doesn't work.memberbswiftly15 Sep '04 - 3:35 
yes, the VB version that I did had no frames. just the one iFrame. It didn't have any more code than your example has.
 
sp2 is the new service pack that tightened up security etc on sp2.
 

GeneralRe: Access Deniedmemberbswiftly15 Sep '04 - 12:12 
ahh..
 
sorry.. noticed one thing.
 
Says Access denied when I load the page that is JUST your code.
 
Line 23, char. 5. was using a variety of sites to test an dthey all had the same error.
 
I tried this on xp, and win2000
GeneralRe: Access DeniedmemberHugo Flores16 Sep '04 - 2:50 
I really couldn't say what is going on. As I told you I tested in win2k and everything is fine. I'm really sorry that I can't provide any more help in this regard.
 
It's not why you're running, it's where your going
It's not what you're dreaming , but what you're going to do
It's not where you're born, it's where you belong
It's not how weak but what will make you strong
GeneralRe: Access Deniedmemberbswiftly16 Sep '04 - 12:05 
Ah, sorry.. I found the answer, unless you have ANOTHER workaround.
 
I can't access the innerhtml of the other form because it will potentially be from a different domain if the user browses off of my domain to another one.
 
Maybe there is a way to emulate the page in the iframe to be on the same domain?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 26 Mar 2003
Article Copyright 2003 by Hugo Flores
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid