OnClientClick One More time

I'm still trying to copy a file from a network server to the web server and then view the file from the web server in another window. Using impersonation the file copy works fine in the server ItemCommand event. However the subsequent client side click event is not quite working.
 
I have a button called btnViewMapImage on my datalist control as follows:

                                       <td>Map Page</td>
                                        <td><asp:TextBox ID="txtMapPage" runat="server" Text='<%# Eval("MapPage") %>' width="50"  ReadOnly="true" />
                                            <asp:Button ID="btnViewMapPage" runat="server"
                                                    CommandName = "ViewMapPage"
                                                    Text="View"
                                                    OnClientClick='<%# "javascript:window.open(" + Utilities.WrapWithSingleQuote(AppConfiguration.WebStagedMapPageSubPath + Eval("MapPage") + "_" + Eval("StagedMapPageID") + ".tif")  + ");" %>'/>
                                        </td>

The code behind to process the button's server side click event is as follows:

    protected void dlstGeneral_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int intPropertyID = GetPropertyID();
        switch (e.CommandName)
        {
            case mstrcViewMapPage:
                // get the map page
                //DataListItem item = e.Item;
                //TextBox txt = (TextBox)item.FindControl(mstrctxtMapPage);
                TextBox txt = (TextBox)e.Item.FindControl(mstrctxtMapPage);
                string strMapPage = txt.Text;
                // get the server path where map pages are stored
                string strPath = AppConfiguration.MapPagePath;
                // map pages always have a .tif extension
                string strFrom = strPath + strMapPage + ".tif";
                TextBox txtTo = (TextBox)e.Item.FindControl("txtStagedMapPage");
                string strTo = txtTo.Text;
                // copy the file to the web server
                string strStagedFile = StagedFileMgr.CopyIn(strFrom, strTo);
                break;
            default:
                break;
        }
    }

When I click the btnViewMapPage button the file gets copied correctly and the client side click event opens a new window with the following correct url

http://localhost/CATS/Images/Staged/37_B7E6F6FC-8A4A-485C-9F0B-A355C154904C.tif

and the message "the page cannot be found".

However if I click IE's refresh button, the page displays as binary data which tells me the file is actually there now but wasn't there at the time of the client side click event..

If I click my view button again (which simply recopies the source file to the same target file name on the web server), the client side click successfully opens the file in a new window with the associated Microsoft Office Document Imaging application.

Why won't it work the first time around? Am I missing an event somewhere? Does the client side click event not fire after the server ItemCommand event? Do I need to invoke some sort of sleep function? How can I check in code that the file is totally there on the web server? I even added some code to use the Exists method of the FileInfo object after the file copy and loop until the result was true, and the onclientclieck event sill resulted in page cannot be found. What's going on here?
rmkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rmkAuthor Commented:
I finally solved the problem now that I have a better understanding of client side and server side events. The trick was to use startup script blocks. According to me reference book Programming Microsoft ASP.NET 2.0 Applications Advanced Topics by Dino Esposito on page 257 "A stratup script block is a piece of script code that executes when the page has been fully initialized but before it is displayed to the user". So I removed the ViewMapPage button's OnClientClick event and used a start up sctipt block. The whole thing is now controlled by the ItemCommand server side event of my ViewMapPage button that is in my datalist control as follows:

    // process item commands
    protected void dlstGeneral_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int intPropertyID = GetPropertyID();
        switch (e.CommandName)
        {
            case "Edit":
                dlstGeneral.EditItemIndex = (int)e.Item.ItemIndex;
                BindGeneralData(intPropertyID);
                break;
            case "Cancel":
                dlstGeneral.EditItemIndex = -1;
                BindGeneralData(intPropertyID);
                break;
            case mstrcViewMapPage:
                // map page files always have a tif extension
                string strExt = ".tif";
                // get all the from pieces
                string strFromPath = AppConfiguration.MapPagePath;
                TextBox txt = (TextBox)e.Item.FindControl(mstrctxtMapPage);
                string strMapPage = txt.Text;
                string strFromFile = strMapPage + strExt;
                string strFromFQ = strFromPath + strFromFile;
                // get all the to pieces
                string strToPath = AppConfiguration.FileSystemStagedMapPageRootPath + AppConfiguration.FileSystemStagedMapPageSubPath;
                string strToFile = strMapPage + "_" + Guid.NewGuid().ToString() + strExt;
                string strToFQ = strToPath + strToFile;
                // copy the file to the web server using impersonation
                StagedFileMgr.Copy(strFromFQ, strToFQ);
                // extract the relative file name for javascript
                string strToRelativeFile = AppConfiguration.WebStagedMapPageSubPath + strToFile ;
                // register the startup script
                // which will execute when the page has been fully initialized
                // but before it is displayed to the user
                Type t = this.GetType();
                if (!ClientScript.IsStartupScriptRegistered(t, mstrcViewMapPage))
                {
                    string strScript = "window.open(" + "'" + strToRelativeFile + "');";
                    ClientScript.RegisterStartupScript(t, mstrcViewMapPage, strScript, true);
                    // this line is purely for debugging purposes
                    // so that we can see the script
                    this.lblViewMagPage.Text = strScript;
                }
                break;
            default:
                break;
        }
    }
0
GranModCommented:
Closed, 500 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.