2010-10-15 31 views
5

我發現這真的很酷的頁面,讓你在Facebook掛鉤到您的網站:See here談起的iFrame從ASP.NET代碼背後

<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> 

我希望能夠調用此iframe在我的網頁(我正在使用ASP.NET),我希望能夠基於變量設置可見性,最重要的是我希望能夠根據變量構建的字符串更改iframe的src,以更改「 www.EXAMPLE.com「轉換爲基於頁面位置的另一個URL。

+0

看一看這個問題:http://stackoverflow.com/questions/3890375/javascript-event -handler-in-asp-net – 2010-10-15 18:53:06

+1

它仍然沒有給我一個關於如何在我的代碼背後與iframe交談的答案..... – Etienne 2010-10-15 18:56:30

回答

19

嘗試添加屬性runat =「server」。這應該給您通過您的代碼隱藏訪問標籤,這將讓您根據您的變量設置其他屬性:

<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> 

這將使你在代碼中訪問到iframe的名字後面。然後,您就可以通過寫之類的語句來操作的事情:

MyIframe.Visible = true; 

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"); 
+0

謝謝,這的確允許我隱藏它,但我將如何或可以我改變了src?它不會選擇src。 – Etienne 2010-10-15 19:23:30

+1

@Etienne:試試這條線。如果在Intellisense中某個屬性不適用於您,則可以使用.Attributes集合手動添加它們。 – 2010-10-15 19:30:14

+1

給了我很多麻煩!不能相信這個伎倆不是更廣泛的知道。 – MAW74656 2013-08-29 20:06:44