在iframe中打开一个asp.net页面

时间:2014-01-24 21:00:35

标签: javascript jquery asp.net vb.net iframe

在我的网站上,我有两个页面......一个是Assistance.aspx,另一个是Help01.aspx
我想要做的是打开iFrame内的第二页,该列位于表格列中,该列属于Assistance.aspx页面内的表格:
为此,我使用以下代码:

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

在我的代码中,我使用了:

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

但是当我来到:

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

它给我一个错误:
Object reference not set to an instance of an object.
因为Me.FindControl("iFrame")没有任何价值 当我从元素中删除runat时,该错误消失了。 为什么呢?

*其他信息*

出于同样的原因,我也使用以下脚本:

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

我从后面的代码中调用它:

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

那个脚本也给我带来了同样的错误。

1 个答案:

答案 0 :(得分:1)

最后我们已经看到这个问题有两个解决方案,这两个解决方案中的每一个都有自己的结果 第一个解决方案是声明iFrame如下:

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

在Button_Click处理程序中,我们使用:

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

在设计视图区域,我们使用:

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

此解决方案工作正常,效果极佳 当然,我们还有附加信息中描述的另一种解决方案,但是需要从设计视图区域的声明中删除runat=”server”
选择并选择...。