?
Solved

.Showdialog() not behaving as expected (c#)

Posted on 2010-01-07
14
Medium Priority
?
1,372 Views
Last Modified: 2012-05-08
Hi
On my webpage i have a button which displays an OpeFileDialog for its Onclick event.  However, the dialogbox is opening behind my web browser.  I had assumed that .ShowDialog() would have opened a modal OpenFiledialog and therefore open ontop of my browser.  If I minimise or move my browser, I can see the OpenFileDialg waiting for me to select  a file or cancel it.

Does anyone know a solution to this or am I doig something wrong?
System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();

dlg.Title = "Select Mailmerge Document";
dlg.Filter = "Word Documents|*.doc*|All File|*.*";

if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
  txtDocumentName.Text = dlg.FileName.ToString();
  txtRowchanged.Text = "true";
}

Open in new window

0
Comment
Question by:WSIT
  • 7
  • 6
14 Comments
 
LVL 12

Expert Comment

by:rajapandian_81
ID: 26201964
Hi,

OpeFileDialog is a windows control. It should not be used in web application.

If you want to select a file from client machine use file upload control.
0
 

Author Comment

by:WSIT
ID: 26202087
I started off using asp:FileUpload, but I was having problems in Firefox v3.  I intended for the user to be able to select a file with the OpenDialog and then display the path of the file in a textbox. The contents of this textbox would be saved as a string in a database.  This worked fine in IE8 but in firefox the displayed text would only ever contain just the filename and not include the directory path.  Hence, resorting to using OpenFileDialog.

I would happily use asp:FileUpload if i could get round the security issues surrounding firefox.  I spent a day investigating the issue and didn't find any workable solutions.  Hence My latest approach.
0
 
LVL 12

Expert Comment

by:rajapandian_81
ID: 26202484
Hi,

Do you want to browse client machine or server?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:Tribmos
ID: 26204690
not sure if it applies to web pages but in normal c# showdialog can take a parameter for the "owner". That ties the two together.

That functionality is what causes the dialog to take focus, and beep at you when you try to click on the main form.

Couldnt hurt to try if works on the webpage.
0
 

Author Comment

by:WSIT
ID: 26208256
rajapandian_81,

I would prefer it if I could browse both client and server.  But if that isn't possible then the client machine would be ok.

Tribmos,

I did have a brief look at that.  I instead of .ShowDialog() I tried .ShowDialg(this).  Howwever, when I try to build the project I get the following two errors:

The best overloaded method match for 'System.Windows.Forms.CommonDialog.ShowDialog(System.Windows.Forms.IWin32Window)' has some invalid arguments      

And

Argument '1': cannot convert from 'MailmergeDocuments' to 'System.Windows.Forms.IWin32Window'      

I'm afraid I am still too new to c# to be able to understand those messages.
0
 
LVL 12

Expert Comment

by:rajapandian_81
ID: 26211969
Hi,

I think asp:FileUpload is the only control to browse client machine and you can move that file to Server.
May I know what is the use of knowing the client machine file path?
0
 

Author Comment

by:WSIT
ID: 26282491
rajapandian_81

My web-based application, along with its MS ACCESS based predecessor, processes various bits of information (eg employee attendance) and then prints off mailmerge documents based of this info.  I was hoping to link each process to a specific mailmerge doc.  With the MS ACCESS app I was able to save the location of the doc as a string in the database, which could then be used to open the doc.  The location of each doc was either entered into a textbox in MS ACCESS or they could browse for the file with an OpenDialogBox that was opened with ACCESS and the file location inserted into the textbox as a string and saved on exit.  The next time the process is run, it can read the location of the file and open the correct mailmerge doc.

I am converting the ACCESS application to a web-based product and hoping to complete the above process with a similar interface.  

The document could be located on either the client machine or the server, hence I assume that I need the client machine path as well.

0
 
LVL 12

Expert Comment

by:rajapandian_81
ID: 26282803
Hi,

The better option will be use file upload control to browse client machine and save that file in server to particular location. Store that server path in database. So that you can use in future.
0
 

Author Comment

by:WSIT
ID: 26283108
Indeed it would.  But the clients are sued to saving their documents in various locations.  Previous attempts to convert the to best practices, such as saving documents in a shared location on the server have not been we received.  I actually risk losing clients if I try to impose such a thing.  I have over 200 clients, each with a slightly different network setup and with different machines.  I have to be able to cater for all of them.

But I do agree, saving the file to the server would be more useful but there is no way I can implement it with minimal complaints from the user.
0
 
LVL 12

Expert Comment

by:rajapandian_81
ID: 26283459
Hi,

I think you misunderstood.

Initially files will be in client machine only. You can allow the user to browse their machine and select file. Once the file is selected, using code you can copy the file to server using below code

FileUpload1.PostedFile.SaveAs("Folder Name" + "FileName");
0
 

Author Comment

by:WSIT
ID: 26284713
Sorry. I think I did misunderstand.

But if they save the file to the server, would that not mean that I would have to know where the folder is on the server so that i can open it at a later date? And that everytime they update the file on the local machine then it will have to be uploaded again?

Sorry for what might be a very silly question.  But I just need to be sure I am on the right track ...
0
 
LVL 12

Expert Comment

by:rajapandian_81
ID: 26285011
Hi,

-- > But if they save the file to the server, would that not mean that I would have to know where the folder is on the server so that i can open it at a later date?
         --  After selecting the file in client machine, you are only going to move that file to server thro code. So you know the server location where you are saving. you can store that folder location and file name to database for future use.

-- > And that everytime they update the file on the local machine then it will have to be uploaded again?
       --  Yes, Everytime user selects the file you can move to server. Once the mailmerge is over, you can delete that file if you want.
0
 

Author Comment

by:WSIT
ID: 26285455
I'll try the method that you have suggested.

Just out of curiosity, is there a reason why the OpenFileDialog opened underneath the web browser.  I know you have already specified that I should use <asp:FileUpload.  But this is the one problem that I have.  if it opened ontop of the browser and with the focus then I shall be sorted :)

Thanks
0
 
LVL 12

Accepted Solution

by:
rajapandian_81 earned 2000 total points
ID: 26285568
Hi WSIT,

OpenFileDialog is not a web control. It will open a File dialog only when your application is in localhost.
When you move the code to server, and try to run application from client PC, the file dialog will not be opened in client machine. So better to avoid.

Refer below URL for more info
http://bytes.com/topic/asp-net/answers/333863-openfiledialog-asp-net-code-behind-illegal
http://forums.asp.net/p/1446385/3292166.aspx
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 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