Change "src" of iframe dynamically in asp.net

Posted on 2012-09-08
Medium Priority
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") = "http://www.cnn.com"

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

ID: 38380331
is this works?

frame1.Attributes.Add("src", "http://www.xyz.com"])

Author Comment

ID: 38380342
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
ID: 38380353
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") = "http://www.cricinfo.com"
    End Sub  

Open in new window

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

LVL 26

Accepted Solution

Alan Warren earned 2000 total points
ID: 38380458
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Me.theframe.Attributes("src") = "http://www.microsoft.com"
  End Sub

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <form id="form1" runat="server">
      <iframe id="theframe" runat="server" src="http://www.google.com.au" frameborder="0" width="600px" height="600px" scrolling="auto" />

Open in new window


Author Closing Comment

ID: 38385444
Thanks alot for the help!!!!!!!!!!!!!

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

850 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