vb.net 在 iframe 内打开一个 asp.net 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21342397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:33:54  来源:igfitidea点击:

Open an asp.net Page inside of an iframe

javascriptjqueryasp.netvb.netiframe

提问by Lefteris Gkinis

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 iFramewhich is in a table column this column belongs to a table which is inside of the Assistance.aspxpage:
For this purpose I use the following code:

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

<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 runatfrom the element. Why?

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

* 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.

该脚本也向我抛出了同样的错误。

采纳答案by Lefteris Gkinis

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:

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

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

And in the Button_Click handler we use the:

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

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 INFORMATIONbut that one needs to delete the runat=”server”from the declaration in the design view area.
Choose and Pick….

此解决方案工作正常,并产生了极好的结果。
当然,我们在附加信息中描述了另一种解决方案,但需要runat=”server”从设计视图区域的声明中删除。
选择和挑选....