Change "src" of iframe dynamically in

Posted on 2012-09-08
Last Modified: 2012-09-10
I am trying to load iframe src at runtime:

 <iframe runat="server" align="left" height="700" id="frame1"  scrolling="yes" width="1013"

Dim frame1 As HtmlControl = CType(Me.FindControl("frame1"), HtmlControl)
frame1.Attributes("src") = ""

This is throwing a null reference exception and I am completely confused about this.  Please advise.  Thanks in advance.
Question by:MaxKroy
    LVL 14

    Expert Comment

    is this works?

    frame1.Attributes.Add("src", ""])

    Author Comment

    Doesn't work.  When I run my debugger it tells me that frame1=nothing, which means during the frame1 declaration and mapping to the html element "frame1" there is no connection, or it can't find the html element.

    Any thoughts?
    LVL 18

    Expert Comment

    by:Rajar Ahmed
    Make sure you have runat server tag on the markup .  Then there is no need to find control directly we can access the object .
     <iframe id="frame1" runat="server">
        Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
            'Dim frame1 As HtmlControl = CType(Me.FindControl("frame1"), HtmlControl)
            frame1.Attributes("src") = ""
        End Sub  

    Open in new window

    Even findcontrol needs server tag else it will still returns nothing .

    LVL 26

    Accepted Solution

    Hi MaxKroy,

    Populate the iframes  runat  attribute, then your will be able to reference it from code behind at the server, other than that any reference to the iframe will return something like: "Error      1      'theframe' is not a member of 'ASP.default12_aspx'."
    <%@ Page Language="VB" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <script runat="server">
      Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.theframe.Attributes("src") = ""
      End Sub
    <html xmlns="">
    <head runat="server">
        <form id="form1" runat="server">
          <iframe id="theframe" runat="server" src="" frameborder="0" width="600px" height="600px" scrolling="auto" />

    Open in new window


    Author Closing Comment

    Thanks alot for the help!!!!!!!!!!!!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Title # Comments Views Activity
    Schema Not Showing in SSDT Database Project 26 37
    HTML table 7 36
    Parameter for stored procedure 6 39
    Javascript - getdate 5 29
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now