500 points --- Retrieving value from Iframe

Hi, I am an ASP Programmer and now just started learning ASP.Net with Vb.Net.

I am trying to put the value of an iframe from WYSIWYG editor, to SQL database, but I am not able to retrieve value from Iframe. Can anyone tell me what is the way to retrieve the value from Iframe, as in ASP it was Request.form("field_name").

Please make suggestions ASAP.

Who is Participating?
khurram, here's a rough-draft of a working example of what I meant.

<form runat="server" ...>
  <asp:TextBox runat="server" id="MyText" TextMode="Multiline" style="display:none;visibility:hidden;"></asp:TextBox>
  <iframe id="MyEditorFrame" src="MyEditor.aspx"></iframe>

Make sure to add a TextChanged event to your MyText control.  It will be called on post back when the editor in the iframe (below) is modified.

  function updateText( obj )
    var container = window.parent; // you can also use "top" if your container is the root frame
    var textbox = container.document.getElementById( 'MyText' );
    textbox.value = obj.value;
    // if the above line doesn't work, then try:
    // textbox.innerText = obj.innerText;
<form runat="server" ...>
  <!-- all the html code for the editor -->
  <!-- on the textbox itself, which will be either an asp:TextBox with textmode set to multiline or a Textarea control
         (which are essentially identical), you need to have an onchange, or an onblur event: -->
  <textarea id="MyTextArea" onchange="updateText(this);"></textarea>

Note that this page does not need to post back ever.  Nor do you need a separate button to propogate the update to the parent frame.  It happens automatically when the user edits text in the editor.  If it works too slow (it shouldn't!) then respond to the onblur event instead of the onchange event.  The onblur event is only fired when the control loses focus, so it may be a bit less reliable, but will prevent it from being slow (which it should not be anyway).

Let me know if I can clarify on any part of this solution.
I'm not quite sure how you're trying to retrieve the value.

It is impossible, both with asp and asp.net to retrieve values directly from an iframe on the server-side.

One solution is to use javascript to update a textbox on the containing frame whenever the text is modified in the editor.  Then, when you post back, you can use the value from your textbox.  Just make sure your textbox is multiline, and invisible.
khurram007Author Commented:
I am not that good in Javascript... can you provide me a working sample?
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Dear khurram,

A working sample might be provided, please give us some of you source/html.

khurram007Author Commented:
Just provide me sample showing how to transfer iframe value to a hidden text field and then retrieving on other page.
U cannot get values from an IFRAME windows...it's not allowed..
If the framed site is not yours (from the same domain as the top domain), you can't know where user is going and what user is doing. This is built in security feature of the browser.

khurram007Author Commented:
I am building the site myself. So no security issue should be there.

The question now is how to transfer iframe value to a hidden text field and then retrieving on other page??? and need a sample code doing that... Remember I am using Iframe for a WYSIWYG editor.

And one more question. I have WYSIWYG editors which use javascript and html text area. Can they work in Asp.net???
try this...
since you are making this site yourself...use javascript to write the value of the field you want in a cookie..and then from the other page where you want to read this value..again use javascript to read the cookie value..
and yes..javascript and textarea work in ASP.NET

here are some sites which can help u with this



What do you mean by getting the "value" from an <iframe>.  If you mean getting the URL of the page showing in the frame, first make the <iframe> a server control by doing this:

<iframe id="frmName" runat="server" src="http://www.someurl.com"></iframe>

This will cause ASP.NET to declare a variable to the HtmlGenericControl class for the iframe.  The variable will be whatever you put in the id="..." attribute.  Then get that value in code like this:

string frameURL = frmName.Attributes["src"]

You can then save that to a database.

Note, however, this will only get the *original* URL loaded when the page loaded.  I don't think it will get the current URL if the user has navigated elsewhere.

By the way, this is standard javascript, and does not in itself have any security issues (as far as I know).

The only thing you need to be careful of, regardless of the solution you implement, is that the users can put dangerous javascript in your textarea in your editor.  You'll need to either do validation, to make sure there's no <script> element in the user-entered code, or enable the safeguard in your web.config.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.