从后面的代码创建动态html文件,并在新窗口中打开

时间:2012-05-23 11:22:57

标签: html vb.net popup

我让我的用户从我的网页上编写自己的HTML。

当他们点击预览按钮时,我的代码需要访问页面上的控件(所以我知道我不能在这里使用web方法)。

它构建HTML并将其作为文件保存在服务器上。

我的问题是: 文件制作完成后,我想在新窗口中自动打开文件,以便用户查看其便捷的工作。

我正在使用vb.net,但很高兴收到c#的答案。

谢谢大家! 。

- = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - 非常感谢Jan !!

我仍然有一个小问题...
我的代码是这样的:

If fileExists Then
        Do Until fileExists = False
            tmpFormPreviewFileName = "Test" & GetRandomNumber() & ".html"
            tmpFormPreviewFilePath = Server.MapPath("~") & "Pages\FormPreviews\" & tmpFormPreviewFileName
            fileExists = My.Computer.FileSystem.FileExists(tmpFormPreviewFileName)
        Loop
    End If

    My.Computer.FileSystem.WriteAllText(tmpFormPreviewFilePath, strHTML, False)


    'Now open the file in a new window
    btnPreview.OnClientClick = String.Format("window.open('/Pages/FormPreviews/{0}', 'myPopup', 'width=400,height=500')", tmpFormPreviewFileName)

所以,问题是 - 在用户点击预览按钮之前我不知道文件名是什么,OnClientClick事件直到用户第二次点击按钮才会触发(当然会创建另一个HTML文件)

- = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - 我仍然无法让弹出窗口工作。 这是我目前的代码:

'create a JavaScript command for opening the file in popup window
    Dim strScript As String = String.Format("window.open('/Pages/FormPreviews/{0}, 'myPopup', 'width=400,height=500')", tmpFormPreviewFileName)

    'register the JavaScript to be executed when web page loads
    ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "openPopup", strScript, True)
    ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "ShowMsg", "javascript:alert('Test Msg');", True)

我的“ShowMsg”成功触发,但它上面的行(弹出代码)似乎不起作用。你能看出出了什么问题吗?

2 个答案:

答案 0 :(得分:1)

1)首先,您必须通过网络访问已保存的HTML文件。 最简单的方法是将它们保存到您网站的某个子文件夹中 - 例如让我们创建一个“/ GeneratedPages”文件夹。

将文件保存到此文件夹时,您需要构建此文件夹的物理路径。您可以使用Server.MapPath方法。

您可能还必须设置访问权限,以允许Web应用程序将数据写入文件夹。

2)现在让我们实现预览窗口的打开:

假设您的预览按钮在ASXP页面中声明如下:

<asp:Button ID="btnPreview" runat="server" Text="Preview" />

然后你只需要将以下语句添加到代码隐藏中,例如到Page_Load方法:

// sample name of one generate file
string pageName = "page1.html";     
btnPreview.OnClientClick = string.Format("window.open('/GeneratedPages/{0}', 'myPopup', 'width=400,height=500')", pageName);

有关使用过的 window.open 方法的更多信息可以在Javascript参考中找到,例如: here

更新: 如果在单击按钮后(在回发中)构建文件,则可以使用RegisterStartupScript方法。您的服务器端单击处理程序可以是以下预览按钮:

protected void btnPreview_Click(object sender, EventArgs e)
{
    string tmpFormPreviewFileName;

    // construct the filename and save the file
    // ...

    // create a JavaScript command for opening the file in popup window
    string script = string.Format("window.open('/Pages/FormPreviews/{0}, 'myPopup', 'width=400,height=500')", tmpFormPreviewFileName);

    // registger the JavaScript to be executed when web page loads
    ClientScript.RegisterStartupScript(GetType(), "openPupup", script, true);
}

答案 1 :(得分:0)

我认为您在JavaScript中缺少引号(见下文)

Dim strScript As String = String.Format("window.open('/Pages/FormPreviews/{0}<-- here  

试试这个:

Dim strScript As String = String.Format("window.open('/Pages/FormPreviews/{0}', 'myPopup', 'width=400,height=500')", tmpFormPreviewFileName)
相关问题