Change "src" of iframe dynamically in asp.net

Greetings,
I am trying to load iframe src at runtime:

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

VB.NET:
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.
MaxKroyAsked:
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" "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
</script>

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

Open in new window

Alan
0
 
dejaanbuCommented:
is this works?

frame1.Attributes.Add("src", "http://www.xyz.com"])
0
 
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?
0
 
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">

    </iframe>
Codebehind
    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


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

Meeran03
0
 
MaxKroyAuthor Commented:
Thanks alot for the help!!!!!!!!!!!!!
0
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.