1

I'm letting my users write their own HTML from my webpage.

When they click the preview button, my code behind needs access to controls on the page (and so I understand I can't use a web method here).

It builds the HTML and saves it as a file on the server.

My question is: Once the file has been made, I want to automatically open the file in a new window for the user to see their handy work.

I'm using vb.net, but happy to receive c# answers.

Thanks folks ! . .

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=--=-=-=--=-=- Thank you very much Jan !!

I still have a slight problem though...
My code goes like this:

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)

So, the problem is - I don't know what the filename will be until after the user has clicked the preview button and the OnClientClick event won't fire until the user clicks the button for the 2nd time (which of course creates another HTML file)

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=--=-=-=--=-=-=-=-=-=-=-=-=-=-=-=- I'm still having problems getting the popup to work. Here's my code at the moment:

'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)

My "ShowMsg" fires succesfully, but the line above it (the popup code) doesn't seem to work. Can you see what's going wrong at all ?

2 Answers 2

1

1) First you have to make the saved HTML files accessible through the web. The simplest way is to save them to some subfolder of your website - e.g. let's create a "/GeneratedPages" folder.

When saving a file to this folder, you will need to construct a physical path to this folder. You can use the Server.MapPath method for it.

You will probably also have to set access rights to allow web application wrtting data to the folder.

2) Now let's implement opening of the preview window:

Assuming your Preview button is declared in your ASXP page as follows:

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

Then you have just to add the following statement to code-behind, e.g. to the Page_Load method:

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

More information about the used window.open method can be found in Javascript reference, e.g. here.

UPDATE: If are constructing the file just after clicking on the button (in a postback), then you can use a RegisterStartupScript method. Your server-side click handler for the preview button can be following:

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Jan - thank you very much for helping ! I've update my original question to illustrate my new problem...
Hi wotney, I've updated my answer according to your question update.
Hi again Jan,I really appreciate you helping me out with this one - thank you very much ! I still have a problem though. I'll update my original question...
Once again - Thank you very much for helping me get that sorted. Very much appreciated Jan !
0

I think your missing a quote in your JavaScript (see below)

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

Try this:

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

1 Comment

Thanks a lot !... it always seems to be those tiny little sneaky bits that take a long time to notice :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.