none
How to access the element with in iframe

    Question

  • In my code i am using I frame control for uploading image , image selection are done in iframe src page after that i want to get the image saved location in  iframe placed page.Is there any way to access the elements with in an iframe .

    Thanks in advance


    Meenu
    Tuesday, November 09, 2010 1:29 PM

Answers

  • You cannot vote on your own post
    0

    hi dear!

    OK

    yes this is DOM,

    but in other way you can handle this in c# as following :

     

    this is your design page source code:

    please point to this note that if you use Iframe as server control as following you have to add   runat="server" attribute as following in design code.

     

    <%@ Page Language="C#" AutoEventWireup="true" 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>Untitled Page</title>
    
    </head>
    
    <body>
      <form id="form1" runat="server">
      <div>
      <iframe src="~/Winter.jpg" id="myIframe" runat="server">myIframContent</iframe>
      
      </div>
      </form>
    </body>
    </html>
    
    

     and in c# code you can handle all of things that you want:

    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    public partial class _Default : System.Web.UI.Page
    {
      protected void Page_Load(object sender, EventArgs e)
      {
        // print on out put current value for iframe
        Response.Write(myIframe.InnerText + "</br>");
    
    
    
        // get src attribute value
        AttributeCollection attrCollection = myIframe.Attributes;
        string mySrc = attrCollection["src"];
    
        // write src attribute value
        Response.Write(mySrc + "</br>");
    
        // set new value for iframe text and write it
        myIframe.InnerText = "this is my new content";
        Response.Write(myIframe.InnerText);
      }
    }
    <br/>
    if this is better answer mark this.<br/>
    
    


    va-ansari
    • Proposed as answer by mohammad ansari Wednesday, November 10, 2010 8:10 AM
    • Marked as answer by Larcolais Gong Tuesday, November 16, 2010 3:46 AM
    Wednesday, November 10, 2010 7:57 AM

All replies

  • Im not sure exactly what your trying to get, but if it is a string, you could use StreamReader, and specify within aniframe

     

    http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx

     

    Tuesday, November 09, 2010 3:19 PM
  • As long as the iframe isn't cross domain, you should be able to access it's contents through the DOM.
    If this answers your question, please mark the question as answered.
    Tuesday, November 09, 2010 4:33 PM
  • As long as the iframe isn't cross domain, you should be able to access it's contents through the DOM.

    + if it's not using different protocols. 

    http://blogs.msdn.com/b/ieinternals/archive/2009/08/28/explaining-same-origin-policy-part-1-deny-read.aspx

    However there are some workarounds (JSONP).

    Tuesday, November 09, 2010 5:02 PM

  • are you familiar with DOM?

     


    va-ansari
    Tuesday, November 09, 2010 9:45 PM
  • No  I am not familiar with DOM

    Meenu
    Wednesday, November 10, 2010 4:02 AM
  • Hi I got the answer by using the following script

    < script type ="text/jscript">

    function SetText()

    {

    document.frames[ "iframe" ].document.forms[ "MyForm" ].Text1.value = "My new Value" ;

    }

    got it from the related posts.

    thank you



    Meenu
    • Proposed as answer by Larcolais Gong Friday, November 12, 2010 6:39 AM
    Wednesday, November 10, 2010 4:52 AM
  • You cannot vote on your own post
    0

    hi dear!

    OK

    yes this is DOM,

    but in other way you can handle this in c# as following :

     

    this is your design page source code:

    please point to this note that if you use Iframe as server control as following you have to add   runat="server" attribute as following in design code.

     

    <%@ Page Language="C#" AutoEventWireup="true" 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>Untitled Page</title>
    
    </head>
    
    <body>
      <form id="form1" runat="server">
      <div>
      <iframe src="~/Winter.jpg" id="myIframe" runat="server">myIframContent</iframe>
      
      </div>
      </form>
    </body>
    </html>
    
    

     and in c# code you can handle all of things that you want:

    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    public partial class _Default : System.Web.UI.Page
    {
      protected void Page_Load(object sender, EventArgs e)
      {
        // print on out put current value for iframe
        Response.Write(myIframe.InnerText + "</br>");
    
    
    
        // get src attribute value
        AttributeCollection attrCollection = myIframe.Attributes;
        string mySrc = attrCollection["src"];
    
        // write src attribute value
        Response.Write(mySrc + "</br>");
    
        // set new value for iframe text and write it
        myIframe.InnerText = "this is my new content";
        Response.Write(myIframe.InnerText);
      }
    }
    <br/>
    if this is better answer mark this.<br/>
    
    


    va-ansari
    • Proposed as answer by mohammad ansari Wednesday, November 10, 2010 8:10 AM
    • Marked as answer by Larcolais Gong Tuesday, November 16, 2010 3:46 AM
    Wednesday, November 10, 2010 7:57 AM
  • The IFrame is for some a very wanted Document Element (especially once with classic ASP), however not with ASP.Net.

    Even with HTML and javascript needs the IFrame some special treatments. 

    Forget it with C# without the normal ASP.Net way. ASP.Net offers you now enough possibilities to show images in a decent way. 

    I see here shown a "response.write", but that places only an (whatever) bitmap response by a webbrowser every time cleaning the previous one (the old way Google was showing its preview images).

     


    Success
    Cor
    Wednesday, November 10, 2010 9:49 AM