Change "src" of iframe dynamically in

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.
Who is Participating?
Alan WarrenConnect With a Mentor Applications DeveloperCommented:
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

is this works?

frame1.Attributes.Add("src", ""])
MaxKroyAuthor Commented:
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?
Rajar AhmedConsultantCommented:
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 .

MaxKroyAuthor Commented:
Thanks alot for the help!!!!!!!!!!!!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.