Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


OnClientClick One More time

Posted on 2006-04-24
Medium Priority
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='<%# "javascript:window.open(" + 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

ID: 16534039
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 = "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;

Accepted Solution

GranMod earned 0 total points
ID: 16545586
Closed, 500 points refunded.
The Experts Exchange
Community Support Moderator of all Ages

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

577 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