OnClientClick One More time

Posted on 2006-04-24
Last Modified: 2012-05-05
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"
                                                    OnClientClick='<%# "" + Utilities.WrapWithSingleQuote(AppConfiguration.WebStagedMapPageSubPath + Eval("MapPage") + "_" + Eval("StagedMapPageID") + ".tif")  + ");" %>'/>

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

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


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?
Question by:rmk

    Author Comment

    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;
                case "Cancel":
                    dlstGeneral.EditItemIndex = -1;
                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 = "" + "'" + 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;

    Accepted Solution

    Closed, 500 points refunded.
    The Experts Exchange
    Community Support Moderator of all Ages

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now