Click here to Skip to main content
15,878,852 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
hi , i am having a problem of changing the src of a iframe dynamically any help could be appreciated ..........thank u...
Posted
Updated 6-Mar-13 18:52pm
v2

try this:
Add runat="server" attribute to your iframe.
ASP.NET
<iframe id="urIframe" runat="server"></iframe>


Once you add runat="server" attrubute to your iframe you can access your iframe from codebehind.

Now add src attribute from codebehind:
C#
urIframe.Attributes.Add("src","http://www.facebook.com");


Good luck.
 
Share this answer
 
Comments
cedenoaugusto 8-Jan-19 15:28pm    
Pavan Kumar, this works well locally, but on server I got an exception.
A possible dangerous Request.Path value was detected on the client (:).
Do you have any idea?
Raje_ 10-Jan-19 21:57pm    
Why are you using colon(:) character in your path? : is not allowed in the path of the url.
XML
Page:

<form id="Form1" method="post" runat="server">
<iframe id="ifrm" runat="server"></iframe>
</form>
Codebehind:

using System.Web.UI.HtmlControls;
public partial class test : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

       ((HtmlControl)(form1.FindControl("ifrm"))).Attributes["src"] = "http://www.yahoo.com";
    }



OR

HTML :

<iframe id="gameframe" runat="server" width="1000" height="500" align="middle" src=""></iframe>


This.gameframe.Attributes("src") = "game url";
 
Share this answer
 
v2
Comments
B.S.S Pavan Kumar 7-Mar-13 0:50am    
hi Azziet thanks for replying ive tried ur solution but its not working,i am using master page inherited page ,in this we should find control in a content placeholder .....
any other solution?????
Member 10059522 16-Jul-13 6:56am    
ya hacı noluyo anlamıyom ki bir türlü olmuyo olmuyo ekleyemiyor
Member 10059522 16-Jul-13 6:58am    
hacılar ne dicem exchange bilen var mı la
Member 10059522 16-Jul-13 6:58am    
bu işte bi bokluk var ama anlamadım ya la :)
also try this code
HtmlControl MyFrame = (HtmlControl)this.FindControl("urIFrameName");
     MyFrame.Attributes["src"] = "yourPageName.aspx";
 
Share this answer
 
Comments
Yogaraj_Muthupandian 3-Nov-14 2:00am    
If we code like this , it shows an error Object reference ha not been set
Add runat="server" attrubute to your iframe and access it on code behind like this :

iframe.src="testpage.aspx"
 
Share this answer
 
Comments
Yogaraj_Muthupandian 3-Nov-14 2:00am    
Page Redirect to the URL , rather than it loads the iframe in the same page itself
VPSingh malhi 11-Feb-15 19:52pm    
i am reading pdf files saved on MSSQL server in form of byte array and trying to display in iframe. I am facing the problem. the first file I had opened is opening again and again even deleting cookies and clear up the temp folder. the code I am using is as below:

Dim RID As String
RID = Request.QueryString("RPTID")
Dim rptProp() As String = RPT.getReport(CInt(RID))
'Me.docContent.Attributes.Item("src") = ""
If rptProp(6) <> "" Then
Filename = rptProp(7)
Me.Filename = Filename
Me.Extension = Mid(rptProp(7), rptProp(7).IndexOf("."c) + 2, rptProp(7).Length)
Dim doc() As Byte = Nothing
doc = RPT.getRepDoc(CInt(RID))
If doc IsNot Nothing Then
If Extension = "pdf" Then
Dim base64String As String = Convert.ToBase64String(doc, 0, doc.Length)
Dim pdfIFrameSrc = "data:Application/pdf;base64,{0}" + base64String

' set the src attribute
Me.docContent.ResolveUrl(pdfIFrameSrc)
End If
End If

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900