I’ve been trying to write code for a user to select a file for downloading using the FileUpload control (in ASP.NET), and then passing that information to another page that has the code to download the file from the user. After some searching, I found a Visual Basic solution that used DirectCast.
DirectCast is a type conversion operation in Visual Basic .NET which is based on inheritance or implementation. Its better to use this than CType when converting objects.
When converting VB code to C#, there is no DirectCast operation available, but standard casting can be used instead. In the following example, I’m trying to pass the contents of a FileUPload control in an ASP.NET page to another web page for processing. Its passed to the other page through a Session variable:
Session ["FileUpload"] = FileUpload1;
Response.Redirect ("Upload_2.aspx");
I found code to Visual Basic code to use the FileUpload object, but it uses DirectCast. The following shows how the code was converted to the equivalent in C#.
Visual Basic
—
Dim objFileUpload As FileUpload = DirectCast(Session("FileUpload"),FileUpload)
C#
—
FileUpload objFileUpload = new FileUpload ();
objFileUpload = (FileUpload) Session ["FileUpload"];
Then I use the code below to download the file, which is typical code for this control. There is no error checking in this example.
if (objFileUpload.HasFile)
objFileUpload.SaveAs ("D:\\uploads\\" + objFileUpload.FileName);
Haissam Abdul Malak posted an article on his blog Ways to Pass Data Between Webforms.
I’m still trying to find a better way of passing variables between a form and modules in App_Code. Passing variables through public properties is the only way I know that works now.