Link to home
Start Free TrialLog in
Avatar of Member_2_7967119
Member_2_7967119

asked on

IFrame on Master Page is not functioning.

Iframe on a ASP.net Master page is giving me an error when I set to the Src property dynamically.

I have a website where the Master page has a Iframe defined. The idea is when ever the user choose a menu option, the  asp.net page to be open is set to the SRC property in the back end of the Master page file.

In the code behind, i am setting the value as follows:
    iMainFrame.Attributes.Add("Src", "http://www.google.com")

I am getting an error "Object reference not set to an instance of an object."

This issue happens only when I am using Master page and it works fine for a asp.net page with a Master file.

Any help is greatly appreciated.

Below is the master page definition.

   <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <div>
           
              <asp:ContentPlaceHolder ID="MainContent" runat="server">
               
                     <iframe id="iMainFrame" src="SearchBuddy.aspx" style="border:none;width:100%;height:2200px;" runat="server" visible="true"></iframe>
                 </asp:ContentPlaceHolder>
       
           
        </div>

 
    </form>
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Try changing
                     <iframe id="iMainFrame" src="SearchBuddy.aspx" style="border:none;width:100%;height:2200px;" runat="server" visible="true"></iframe>

Open in new window

to
                     <iframe id="iMainFrame" ClientIDMode="Static" src="SearchBuddy.aspx" style="border:none;width:100%;height:2200px;" runat="server" visible="true"></iframe>

Open in new window


When you use Master Pages, additional containers are added and by design ASP.Net will prefix your control name with those container names. To by pass this, you can force static(i.e. ASP.Net does not modify control's id that you have assigned) Ids.
Avatar of Member_2_7967119
Member_2_7967119

ASKER

Nope it is not working.
Alright. Please check the console (CTRL + SHIFT + I or F12) and tell me if you see any errors in there.
I found the issue.

When I moved the iframe definition outside the ContentPlaceHolder, it did work.


<iframe id="iMainFrame" src="SearchBuddy.aspx" style="border:none;width:100%;height:2200px;" runat="server" visible="true">
<asp:ContentPlaceHolder ID="MainContent" runat="server"> </asp:ContentPlaceHolder>
</iframe>
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.