Developing Matt

My Technical Journal

Making the iFrame Source dynamic

leave a comment »

It never ceases to amaze me how simple something should be yet complicated it becomes, either due to the fact that it is after 11:00 PM and I’m falling asleep or that I’m not as smart as I think I am. Either way, here is my problem and solution. I want to set the source of an IFrame dynamically. I’m obviously missing a concept here…or the IFrame folks didn’t think this would be a useful property to publically display. What didn’t work:

~Setting a function in the code behind and applying the src property to grab that functions result (as works so well in other things).
~Javascript might have worked as suggested here but since my control is in a master page then it didn’t work or I didn’t try hard enough. Besides, his suggestion was to create another page and call that page using querystrings, etc and I didn’t want the overhead.
~This gent offered a solution that didn’t apply: creating a rotator control inside of the page that handled a rotating ad-like feature. It wasn’t what I wanted but there it is if you do.
~Setting the iframe’s attribute didn’t do anything either iframeid.attribute(“src”)=”page.aspx”  I think in a previous version this wasn’t read only.

    The winner goes to Harish Ranganathan. He explains how to do it so even a guy like me can do it. Here’s the linky linky and here’s the solutions that should work:

    Set Frame in page: <IFRAME id=”frame1″ scrolling=”auto” runat=”server”></IFRAME>
    In code behind add Protected WithEvents frame1 As System.Web.UI.HtmlControls.HtmlGenericControl
    and Dim frame1 As HtmlControl = CType(Me.FindControl(“frame1”), HtmlControl)
    So that you can set the src property frame1.Attributes(“src”) = http://website.com

    This didn’t work due to it not finding the frame1. But instead of troubleshooting it further Harish gave the easiest solution of all:

    SOLUTION:
    set the iframe as a server control
    frame1.Attributes.Add(“src”, “www.webpage.com”)

    That’s right, after close to two hourse my solution came in one line of code. *sigh* Oh well. I liked this guy so much I added him on my blogroll.  Here’s a pic of our friend who helped us in our time of need.  He even looks like a nice guy.

    Harish

    Advertisements

    Written by matt

    October 12, 2007 at 11:05 pm

    Posted in Asp.net

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    %d bloggers like this: