CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

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

 



Comments

Peter van Ooijen said:

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
# December 3, 2003 4:43 AM

Andrew said:

I'm Administrator and using IE(MyIE;-)...
# December 3, 2003 5:18 AM

Peter van Ooijen said:

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.
# December 3, 2003 5:28 AM

Andrew said:

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.
# December 3, 2003 5:29 AM

Peter van Ooijen said:

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.
# December 3, 2003 6:56 AM

Michael Harris said:

'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.
# December 3, 2003 8:05 AM

Peter van Ooijen said:

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.
# December 3, 2003 12:34 PM

jean said:

Nice one Peter!! code works beautifully

:)
# April 21, 2004 11:01 AM

Peter van Ooijen said:

# April 21, 2004 2:15 PM

vanni said:

A question :
How can I open dialog to choose a directory?

Thanks
# June 15, 2004 12:49 PM

Peter van Ooijen said:

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.
# June 16, 2004 2:12 AM

nguyen phuong nam said:

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
# January 4, 2005 7:39 PM

Peter van Ooijen said:

Yes, ActiveX must be allowed. ANother reason to use the HTML way : http://dotnetjunkies.com/WebLog/petergekko/archive/2003/12/03/4224.aspx
# January 5, 2005 2:36 AM

Peter's Gekko said:

Yesterday I blogged on the way to start a fileopen dialog from a web-page. Thanks to the feedback we...
# September 20, 2005 7:24 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

This Blog

Syndication

News