Link to home
Start Free TrialLog in
Avatar of chuang4630
chuang4630

asked on

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)?

Avatar of raterus
raterus
Flag of United States of America image

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")
Avatar of chuang4630
chuang4630

ASKER

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,
I mean sample code?
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
May I see the "Register" Line (in HTML)?
Are you creating the user control?
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
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>

ASKER CERTIFIED SOLUTION
Avatar of raterus
raterus
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks