Talking to iFrame from ASP.NET code behind

50,052

Try adding the attribute runat="server". This should give you access to the tag via your codebehind, which will let you set other attributes according to your variable.:

<iframe id="MyIframe" runat="server" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.EXAMPLE.com%2F&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=recommend&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;" allowTransparency="true"></iframe>

This will give you access to your iframe by name in code behind. You'll then be able to manipulate things by writing statements like:

MyIframe.Visible = true;

and

MyIframe.Attributes.Add("src", "http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.EXAMPLE.com%2F&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=recommend&amp;colorscheme=light&amp;height=21");
Share:
50,052
Etienne
Author by

Etienne

Updated on November 23, 2020

Comments

  • Etienne
    Etienne over 3 years

    I found this really cool page that allows you to hook up facebook into your site: See here

    <iframe id="MyIframe" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.EXAMPLE.com%2F&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=recommend&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;" allowTransparency="true"></iframe>
    

    I want to be able to call this iframe in my page (I am using ASP.NET) and I want to be able to set the visibilty based on a variable and most important I want to be able to change the src of the iframe based on a string that is build up by variables to change the "www.EXAMPLE.com" to another URL based on the location of the page.

  • Etienne
    Etienne over 13 years
    Thanks, that does allow me to hide it, but how will I or can I change the src? It is not picking the src up....
  • Ryan Hayes
    Ryan Hayes over 13 years
    @Etienne: Try that line. If an attribute isn't readily available to you in Intellisense, you can add them manually with the .Attributes Collection.
  • MAW74656
    MAW74656 over 10 years
    Saved me a lot of trouble! Can't believe this trick isn't more widely known.