Lefteris Gkinis
Lefteris Gkinis

Reputation: 1259

Open an asp.net Page inside of an iframe

In my web site I have two pages... one is the Assistance.aspx, and the other is Help01.aspx
What I want to do is to open the second page inside the iFrame which is in a table column this column belongs to a table which is inside of the Assistance.aspx page:
For this purpose I use the following code:

<iframe id="iFrame" runat="server"  class="tablecolumndiv" >

And in my code behind I use:

Protected WithEvents iFrame As System.Web.UI.HtmlControls.HtmlGenericControl<br/>
Public frame1 As HtmlControl = CType(Me.FindControl("iFrame"), HtmlControl)

But when I come to the:

Private Sub button1_Click(sender As Object, e As System.EventArgs) Handles button1.Click
        frame1.Attributes("src") = "/Pages/Support/Asp/Help01.aspx"
End Sub

It throws me an error of :
Object reference not set to an instance of an object.
Because the Me.FindControl("iFrame") has value of nothing
That error eliminated when I delete the runat from the element. Why?

* ADDITIONAL INFORMATION *

I also use the following script for the same reason:

<script type="text/javascript">
    function setPage(frame, pName) {
        document.getElementById(frame).src = pName;
    }
</script>

Which I call it from my code behind:

Private Sub button1_Click(sender As Object, e As System.EventArgs) Handles button1.Click
        Dim myNewAsp As New AspNetSqlProvider
        myNewAsp.InitializeSite(sender, e)
        Dim url As String = "/Pages/Support/Asp/Help01.aspx"
        Dim urlURI As String = HttpContext.Current.Request.Url.AbsoluteUri
        Dim urlPath As String = HttpContext.Current.Request.Url.AbsolutePath
        Dim myServerName As String = Strings.Left(urlURI, urlURI.Length - urlPath.Length)
        root_url = myServerName
        Dim assist As New Assistance
        Dim frameName As String = "iFrame" 'assistiFrame.ID
        iPageLoad(frameName, sender, root_url + url)
End Sub
Public Sub iPageLoad(FrameId As String, sender As Object, msg As String)
        Dim cstype As Type = Me.GetType()
        Dim innerMess As String = msg
        Dim url As String = HttpContext.Current.Request.Url.AbsoluteUri
        Dim script As String = "setPage('" + FrameId + "', '" + innerMess + "')"
        If Not Page.ClientScript.IsStartupScriptRegistered(Me.GetType(), "iPage") Then
            Page.ClientScript.RegisterStartupScript(cstype, "iPage", script, True)
        End If
End Sub

That script also throws me the same error.

Upvotes: 3

Views: 8598

Answers (1)

Lefteris Gkinis
Lefteris Gkinis

Reputation: 1259

Finally we’ve seen that this issue have two solutions, every one of those two solutions have it’s own results.
The first solution is to declare the iFrame like this:

Protected WithEvents iFrame As System.Web.UI.HtmlControls.HtmlGenericControl

And in the Button_Click handler we use the:

iFrame.Attributes("src") = "/Pages/Support/Asp/Help01.aspx"

And in the design view area we use the:

<iframe id="iFrame" runat="server" class="tablecolumndiv" >

This solution works fine, and produces an excellent result.
Of course we have another solution described in the ADDITIONAL INFORMATION but that one needs to delete the runat=”server” from the declaration in the design view area.
Choose and Pick….

Upvotes: 2

Related Questions