Link to home
Start Free TrialLog in
Avatar of rmk
rmk

asked on

OnClinetClick and window.open

Why doesn't my OnClientClick event work?

Here' my asp.net 2.0 code:
                                <td>Map Page</td>
                                <td><asp:TextBox ID="txtMapPage" runat="server" Text='<%# Eval("MapPage") %>' width="50"  ReadOnly="true" />
                                &nbsp;&nbsp;<asp:Button ID="btnViewMapPage" runat="server"
                                            CommandName = "ViewMapPage"
                                            Text="View"
                                            OnClientClick='<%# "javascript:window.open(\"" + AppConfiguration.StagedMapPagePath + Eval("StagedMapPageID") + "_" + Eval("MapPage") + ".tif" + "\"" + ");" %>'/>
                                </td>

Here's the code generated by asp.net 2.0

                                <td>Map Page</td>
                                <td><input name="ctl00$ContentPlaceHolder1$dlstGeneral$ctl01$txtMapPage" type="text" value="37" readonly="readonly" id="ctl00_ContentPlaceHolder1_dlstGeneral_ctl01_txtMapPage" style="width:50px;" />
                                &nbsp;&nbsp;<input type="submit" name="ctl00$ContentPlaceHolder1$dlstGeneral$ctl01$btnViewMapPage" value="View" onclick="javascript:window.open(&quot;c:\Temp\Staged\1_37.tif&quot;);" id="ctl00_ContentPlaceHolder1_dlstGeneral_ctl01_btnViewMapPage" />
                                </td>

The file c:\Temp\Staged\1_37.tif definitely exists.

When does the OnClientClick event actually fire in the page life cycle?
Avatar of existenz2
existenz2
Flag of Netherlands image

onclick="javascript:window.open(&quot;c:\Temp\Staged\1_37.tif&quot;);" wil not work because of the &quot;.

Problem is here:
OnClientClick='<%# "javascript:window.open(\"" + AppConfiguration.StagedMapPagePath + Eval("StagedMapPageID") + "_" + Eval("MapPage") + ".tif" + "\"" + ");" %>'/>

Change it to:
OnClientClick="<%# "javascript:window.open('" + AppConfiguration.StagedMapPagePath + Eval("StagedMapPageID") + "_" + Eval("MapPage") + ".tif" + "');" %>"/>
Avatar of rmk
rmk

ASKER

When I try your suggestion I get "server tag is not well formed". These " and ' are driving me nuts.
OnClientClick="javascript:window.open(' <%# AppConfiguration.StagedMapPagePath + Eval("StagedMapPageID") + "_" + Eval("MapPage") + ".tif" %> ');" />

looks a bit better I think, should work also.
Avatar of rmk

ASKER

I still get the same error
Avatar of rmk

ASKER

Okay, so now i've used a wrapper function to get the single quotes

                                            OnClientClick='<%# "javascript:window.open(" + Utilities.WrapWithSingleQuote(AppConfiguration.StagedMapPagePath + Eval("StagedMapPageID") + "_" + Eval("MapPage") + ".tif")  + ");" %>'/>

and asp.net 2.0 generates

                                <td>Map Page</td>
                                <td><input name="ctl00$ContentPlaceHolder1$dlstGeneral$ctl01$txtMapPage" type="text" value="37" readonly="readonly" id="ctl00_ContentPlaceHolder1_dlstGeneral_ctl01_txtMapPage" style="width:50px;" />
                                &nbsp;&nbsp;<input type="submit" name="ctl00$ContentPlaceHolder1$dlstGeneral$ctl01$btnViewMapPage" value="View" onclick="javascript:window.open('C:\Temp\Staged\1_37.tif');" id="ctl00_ContentPlaceHolder1_dlstGeneral_ctl01_btnViewMapPage" />
                                </td>

but the OnClinetClick event still won't fire. Do I have to do something special because the file is not in a wwwroot folder?
Is it placed between <form> tags?
Avatar of rmk

ASKER

Yes. If I change my path for C:\Temp\Staged to //localhost/cats/images/staged then the onclientclick event works. However that causes some other security issues.

To put the whole thing in perspective this is what I'm trying to do:

- from the datalist control I can manufacture the fully qualified name of a file on a network server; at that time I can also manufacture fully qualified name that this file will be copied to on the web server
- in the click event of the btnViewMapPage button which is in the datalist control, I use impersonation to access the file as follows:

        IntPtr token = IntPtr.Zero;
        WindowsImpersonationContext impersonatedUser = null;

        try
        {
            // Create a token for DomainName\Bob
            // Note: Credentials should be encrypted in configuration file
            bool result = LogonUser(AppConfiguration.FileSstemUser,
                                    AppConfiguration.FileSstemDomain,
                                    AppConfiguration.FileSstemPassword,
                                    LogonSessionType.Network,
                                    LogonProvider.Default,
                                    out token);
            if (result)
            {
                WindowsIdentity id = new WindowsIdentity(token);
                etc copy the file to the web server

- still using impersonation, I copy the file from the network server to c:\Temp\Staged\ on the web server. As far as I know I can't copy it to a folder under the web application because the windows account that I am using for impersonation has no privileges for inetpub\wwwroot\...

- finally the OnClientClick event of the btnViewMapPage is supposed to open a new window and show the file that was just copied to the web server. It now seems that the IUSR account that runs the web application does not have privileges to the c:\Temp\Staged folder.

That leaves me with 2 questions:

1) what can I use for a quick fix to demo this thing to my client on Monday morning
2) what is the best practices design for this situation
ASKER CERTIFIED SOLUTION
Avatar of existenz2
existenz2
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rmk

ASKER

existenz2 - Thanks for all you help. I managed to get thru my demo just fine and I'll be accepting your answer in a few minutes. If you have the time I would appreciate your comments or suggested links for best practices for my scenario, i.e.

- the wep app needs to copy a file from a network server to a staging area and then view that file form the staging area
- where should that staging area be: a folder under the web app, some other folder on the web server, or what
- should the IUSR account ever be given privileges beyond its default privileges