Peter's Gekko

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
How to use a windows dialog in your webpage

When you want to upload a file to the dnj site or the .text blog admin, a real Windows file open dialog pops. Quite cool and a piece of functionality I wanted for one of my own apps. Getting that done was, thanks to COM, easier than I had anticipated. So let me share this with you.

A (file)dialog in a webpage is run by the browser. It is client-side code and has to a snippet of script. Personally I am not a great fan of scripting (too many things can go wrong at runtime) so the script snippet should be as small as possible. From the server side code of a web-page you can inject snippets of script from your C# (or vb.net) code. The nice way of creating the script that way is that you can leave a lot of the decision logic on the server and generate the smallest snippet imaginable. What I want to do is start a dialog and copy the name of the chosen file into a textbox. The Javascript to do that :

<Script language=JavaScript>

function BrowseClick(){
   var dialog = new ActiveXObject('MSComDlg.CommonDialog');
   dialog.Filter = 'All files (*.*)|*.*| ';
   dialog.MaxFileSize = 260;
   dialog.ShowOpen();
   document.forms[0]['TextBox1'].value = dialog.FileName;
return false;}

</Script>

The function creates a commondialog COM object which wraps up all funtionality and exposes the essentials to script.  A larger story on all its possibilities is on MSDN, here I only set the required properties. The ShowOpen method of the COM object executes it after which the selected filename is copied into TextBox1.

This script is only added to the page if there is a chance that it will be run, that is in the selected item template of a datalist. In my code it is a part of the datalist's ItemDataBound event.

System.Text.StringBuilder sb = new System.Text.StringBuilder("<Script language=JavaScript> function BrowseClick(){");
sb.Append("var dialog = new ActiveXObject('MSComDlg.CommonDialog');");
sb.Append("dialog.Filter = 'Alle bestanden (*.*)|*.*| ';");
sb.Append("dialog.MaxFileSize = 260;");
sb.Append("dialog.ShowOpen();");
sb.Append(String.Format("document.forms[0]['{0}'].value = dialog.FileName;", TextBox1.ClientID));
sb.Append("return false;} </Script>");

The script function is built using a stringbuilder. To insert the correct name of the textbox I use the ClientID property of the textbox control. This is the ID of the control in the browser, inside a template of a datalist or datagrid this ID will read something like "DataList1__ctl4_TextBox1" instead of  just "TextBox 1".

RegisterClientScriptBlock(scriptKey, sb.ToString());

The generated script is added to the page by a call to the RegisterClientScriptBlock method. After that it can be used by the controls.

ButtonLinkNaarDocumentatie.Attributes.Add("OnClick", "return BrowseClick();");

In the last line a click handler is added to a button. The clickhandler will call the registered scriptfunction. As that function returns a false the postback initiated with the buttonclick will be canceled. All processing is done on the client, there's no reason yet for any server involvement.

The main thing to watch is the casing of your script. JavaScript is, like C#, also case-sensitive.

Blog on,

Peter

 


Posted 12-02-2003 11:51 AM by pvanooijen

[Advertisement]

Comments

Andrew wrote re: How to use a windows dialog in your webpage
on 12-03-2003 3:20 AM
It's good idea but broken:
>new ActiveXObject('MSComDlg.CommonDialog');
throws error:
>Automation server can't create object.
checked on winxp/2003, running from local machine.
Any suggestions?

Thanks.
Peter van Ooijen wrote re: How to use a windows dialog in your webpage
on 12-03-2003 4:43 AM
There are gotcha's:
- The browser used must support ActiveX. IE does
- The user must have the rights to access the UI. Not always the case.

Peter
Andrew wrote re: How to use a windows dialog in your webpage
on 12-03-2003 5:18 AM
I'm Administrator and using IE(MyIE;-)...
Peter van Ooijen wrote re: How to use a windows dialog in your webpage
on 12-03-2003 5:28 AM
Setting the security level for the local intranet in IE to high will produce your error. Set the level to medium or lower and you can create the dialog.
Andrew wrote re: How to use a windows dialog in your webpage
on 12-03-2003 5:29 AM
After I've set up configuration for local intranet
to "Initialize and script AciveX controls not marked as safe", other error encountered:
>The control could not be created because it is not
properly licensed.

Strange :-(
Resume- it will definitely not work in default user
configuration anyway.
Peter van Ooijen wrote re: How to use a windows dialog in your webpage
on 12-03-2003 6:56 AM
It is always tricky to use ActiveX things and stepping out of the browser is something which will give many administrators the shivers. My "solution" does work with security set to the default medium level. All the details are up to MS who implemented the Common dialog ActiveX wrapper.
Michael Harris wrote re: How to use a windows dialog in your webpage
on 12-03-2003 8:05 AM
'MSComDlg.CommonDialog' is a licensed control. It works from client side IE hosted script only if a Microsoft dev tool (like VStudio) is installed on the client to provide a design time license.
Peter van Ooijen wrote re: How to use a windows dialog in your webpage
on 12-03-2003 12:34 PM
That's clear. Indeed the whole thing doesn't work on a machine without tools. This discusson is not unique

http://www.tek-tips.com/gviewthread.cfm/lev2/4/lev3/29/pid/351/qid/53330

Which does show a very nice and simple solution. I'll create a new post based on that.
jean wrote re: How to use a windows dialog in your webpage
on 04-21-2004 11:01 AM
Nice one Peter!! code works beautifully

:)
Peter van Ooijen wrote re: How to use a windows dialog in your webpage
on 04-21-2004 2:15 PM
vanni wrote re: How to use a windows dialog in your webpage
on 06-15-2004 12:49 PM
A question :
How can I open dialog to choose a directory?

Thanks
Peter van Ooijen wrote re: How to use a windows dialog in your webpage
on 06-16-2004 2:12 AM
There is another common dialog to do that. I'm not sure of it's name, can't be to hard to find. But also that will only work with the activeX installed.
nguyen phuong nam wrote re: How to use a windows dialog in your webpage
on 01-04-2005 7:39 PM
Here, I help you to open dialog and choose a directory.
contact me: phuongnam3000@yahoo.com

PS: U must configue IE to allow run ActiveX and some code in Microsoft.

http://search.microsoft.com/search/results.aspx?View=msdn&st=a&qu=BrowseForFolder&c=0&s=1
Peter van Ooijen wrote re: How to use a windows dialog in your webpage
on 01-05-2005 2:36 AM
Yes, ActiveX must be allowed. ANother reason to use the HTML way : http://dotnetjunkies.com/WebLog/petergekko/archive/2003/12/03/4224.aspx
Peter's Gekko wrote The proper way to open a file dialog from your webpage
on 09-20-2005 7:24 AM
Yesterday I blogged on the way to start a fileopen dialog from a web-page. Thanks to the feedback we...
SathishkumarB wrote re: How to use a windows dialog in your webpage
on 08-05-2008 5:47 AM

Nice , code works thanks alot

parke wrote re: How to use a windows dialog in your webpage
on 09-07-2008 12:34 PM

Nice one Peter!! code works beautifully

Nilesh wrote re: How to use a windows dialog in your webpage
on 09-22-2008 9:34 AM

very Good  work

but how can we allow multiple file selection

rüya tabiri wrote re: How to use a windows dialog in your webpage
on 09-26-2008 9:20 PM

Thank Youuu

Alex wrote re: How to use a windows dialog in your webpage
on 02-03-2009 12:28 AM

Thank You!!!

Add a Comment

(required)  
(optional)
(required)  
Remember Me?