I am writing a ASP.NET 2.0 web based application in which
I want the user to create & select a Folder using Windows Explorer.After the user creates and selects the folder, on pressing OK I should be able to get the PATH of the folder for storing in a database.
The problem is that there is nothing like FolderBrowserDialog in WinForms which allows me to select a Folder and get its path.
I got a good article in Code project but its written in VC++ 6.0.The article is
http://www.codeproject.com/dialog/cfolderdialog.asp
Can someone provide me the equivalent code in VC# Visual Studio 2005 or help me write a similar code. It will be greatly appreciated.
Thanks in advance.
Loading
Mike GoldPosted Oct 19, 2007, 4:07 PM
the trick here is to use the input field of the type 'File' in html and then call dynamically created javascript to fire a client callback.
type="File" />
In C#, Implement
ICallbackEventHandler in the Page and get the filename in the RaiseCallbackEvent and dynamically register the connection of javascript to the server as shown in the code below:
using
System;using
System.Data;using
System.Configuration;using
System.Collections;using
System.Web;using
System.Web.Security;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using
System.Web.UI.HtmlControls;public
partial class TestFilePicker : System.Web.UI.Page, ICallbackEventHandler{
protected void Page_Load(object sender, EventArgs e){
// Get javascript function from the // server that is capable of connecting // to our server. ReceiveServerData is // the javascript method we will call when // we are done processing the data string reference = this.ClientScript.GetCallbackEventReference( this, "arg", "ReceiveServerData", "");
// Generate the javascript that can connect // to the server string callBackScript = @" function CallServer(arg, context){"
+ reference + ";}";
// Put the javascript on the page this.ClientScript.RegisterClientScriptBlock( this.GetType(), "CallServer", callBackScript, true); string callToServer = @"function GetSelectedPath(obj)
{
filePath = obj.value;
CallServer(obj.value, '');
}
"
; this.ClientScript.RegisterClientScriptBlock( this.GetType(), "callToServer", callToServer, true);}
///{
Session["path"] = eventArgument;}
///{
string path = Session["path"] as string; return path; // throw new NotImplementedException();}
}