[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 747
  • Last Modified:

How do I dynamically embed the iframe into the ASP.NET code?

How do I dynamically embed the iframe into the ASP.NET code?

For example, area A is a frame (iframe id = myFrame), and area B reguar webform.
How do I change the iframe src in area A dynamically (in the code behind)?

0
chuang4630
Asked:
chuang4630
  • 6
  • 3
1 Solution
 
raterusCommented:
add an id="myIframe" and runat="server" to the <iframe ... ></iframe> and you will be able to access it from code

...

myIframe.attributes.add("src", "http://www.mydomain.com")
0
 
chuang4630Author Commented:
But I cannot find iframe control in ASP.NET.
<asp:iframe id="myFrame" runat="Server"></asp>
or
<iframe id="myFrame" runat="Server"></iframe>

???

I am away from my development PC so just cannot test it. Do you have tested code to share?

Thanks,
0
 
chuang4630Author Commented:
I mean sample code?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Sammy AgeilCommented:
in HTML
        <iframe id="myIframe" runat="server" visible="true" width="400px" height="600px"></iframe>

In code behind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim iFrameControl As HtmlControl = CType(Me.FindControl("myIframe"), HtmlControl)
        iFrameControl.Attributes.Add("src", "http://google.ca")
End sub

once your page load the iframe will load google.ca

HTH
0
 
chuang4630Author Commented:
May I see the "Register" Line (in HTML)?
0
 
chuang4630Author Commented:
Are you creating the user control?
0
 
raterusCommented:
there are not asp:somecontrol versions for every html tag out there, iframe is one of them.  For controls like this, you can still add an id/runat="server", and if you wanted to use them in code, they are considered a HtmlGenericControl
0
 
chuang4630Author Commented:
I received a runtime error:  Object reference not set to an instance of an object. And point to the line of
frameTop.Attributes["src"] = "http://search.msn.com";

What is wrong with the code?

Here is the code:
   
//protected System.Web.UI.HtmlControls.HtmlGenericControl frameTop;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HtmlControl frameTop = (HtmlControl)this.FindControl("frameTop");
            frameTop.Attributes["src"] = "http://search.msn.com";

        }
    }

<asp:Content ID="Content2" ContentPlaceHolderID="TopArea" Runat="Server">
<iframe id="frameTop" runat="Server"  title = "SOMETHING" width = 100%></iframe>
</asp:Content>

0
 
raterusCommented:
You're doing too much, as in the HtmlControl cast stuff...It also helps to know if you are using asp.net 2.0, and from your example I can tell you are, anyway...

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            frameTop.Attributes.Add("src", "http://search.msn.com");
        }
    }
0
 
chuang4630Author Commented:
Thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now