How to make dynamic iframe src in html?

zahraali

Member
Joined
Jun 9, 2010
Messages
7
Programming Experience
Beginner
How to make dynamic iframe src in html?


I use asp.net and I have page which consist the games
When the user click one of them the new page will open which consist of ifream and the src of iframe changes depend on the game use it

thnx for ur help
 
If I understand you correctly you are poping-up another ASPX page which consists of an iframe.

If so on the page with the games on you should set the hyperlinks as followed:

VB.NET:
Expand Collapse Copy
<a href="/popup.aspx?Game=Hangman">Hangman</a> 
<a href="/popup.aspx?Game=TicTacToe">TicTacToe</a> 
etc.

Then in the popup window you can use the parameter than you sent in the hyperlink to edit the Source of the iframe:

VB.NET:
Expand Collapse Copy
'Gets the parameter from the URL
Dim ChosenGame As String = Request.QueryString("Game")

'
        Literal1.Text = "<IFRAME NAME='embeddedFrame'" & _
  " WIDTH='100%' HEIGHT='360' frameborder='0' SRC='/Games/" & _
  ChosenGame & ".html'> </iframe>"

Then in the Source view of the ASPX page add the following where you would like the iframe to appear:

VB.NET:
Expand Collapse Copy
<asp:Literal ID="Literal1" runat="server"></asp:Literal>

You will obviously have to edit this accordingly as I have no idea in what format your games are stored.
 
You can use the Designer to add the iframe and refer to it serverside by adding the runat attribute:
HTML:
Expand Collapse Copy
<iframe id="GameFrame" runat="server" height="500" width="500"></iframe>
VB.NET:
Expand Collapse Copy
Me.GameFrame.Attributes("src") = "the game url"
 
Thanks to all about your solutions
I will tray they and then return to gives you the feedback
I’m sorry about late but I had bad internet connection on the last days
 
JohnH

Your solution doesn’t work with me. I don’t know why ?

code:
----------------------------------------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.GameFrame.Attributes("src") = Session("Gamelink")
End Sub
-------------------------------------------------

The error msg is:
Error 13 'GameFrame' is not a member of 'GamePlayer'.
 
SteveBarker
i was tray your solution, but see the result

Server Error in '/ITAwebsite' Application.
--------------------------------------------------------------------------------

HTTP Error 404 - Not Found.

--------------------------------------------------------------------------------
Version Information: ASP.NET Development Server 8.0.0.0
 
I wanna the src of iframe consist the link of website which include the game.
My Games.aspx page consist data list which retrieve the name and thumbnail image for the game from data in SQL server. When the user click on the name of game or thumbnail image the Player.aspx image will open with iframe which consist the game which select by the user.
So I wanna just one code make that


For more information:
This is the datalist code in Games.aspx page:
<asp:DataList ID="DataList2" runat="server" align="center" BorderColor="#404040"
BorderStyle="Outset" BorderWidth="2px" Height="227px" HorizontalAlign="Center"
RepeatColumns="3" RepeatDirection="Horizontal" Width="788px">
<AlternatingItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
<br />
<asp:ImageButton ID="ImageButton1" runat="server" Height="100px" ImageUrl='<%# Eval("Gamephoto") %>'
OnClick="ImageButton1_Click" Width="100px" /><br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/GamePlayer.aspx?Game=1" Text='<%# Eval("GameName") %>' >HyperLink</asp:HyperLink><br />
<br />
</ItemTemplate>
</asp:DataList >


In Games.aspx.vb
#Region "[ Load Games ]"
Private Sub GetGames()

Dim objCommand As SqlCommand
Dim objConnection = New SqlConnection(connString)
Dim objDataSet1 As DataSet
Dim objAdapter1 As SqlDataAdapter
Dim datasrc As SqlDataSource
objConnection.Open()
objCommand = New SqlCommand()
objCommand.Connection = objConnection
objCommand.CommandText = "GetGamesinfo"


objCommand.CommandType = CommandType.StoredProcedure
objDataSet1 = New DataSet()
objAdapter1 = New SqlDataAdapter(objCommand)
datasrc = New SqlDataSource

objAdapter1.Fill(objDataSet1)

'Assign the retrieved value to the control
DataList2.DataSource = objDataSet1
DataList2.DataBind()


objConnection.Close()
objConnection.Dispose()

End Sub
#End Region

First I wanna save the id of game selected in the session but I don’t how I did that.
Second wants, when the user click on the game which he want to play the Player.aspx page will open which consist iframe to open the game inside it
So I wanna the src of Iframe change dynamically depends on the game selected.
I hope it is clear info.
 
You can use the Designer to add the iframe and refer to it serverside by adding the runat attribute:
HTML:
Expand Collapse Copy
<iframe id="GameFrame" runat="server" height="500" width="500"></iframe>
VB.NET:
Expand Collapse Copy
Me.GameFrame.Attributes("src") = "the game url"

thanksssssssssssssssssssssssssssssssssssssssss

johnH its work with me now

thank u so muchhhhhhhhhhhhhhhhhhhhhhhhhhh :D:D:D:D
 
Back
Top