title: How to pass a Global String Variable from the C# code behind of an ASP.NET page to a classic ASP page?
body: <p>All, I am maintaining some old .ASP code for a website that requests a work order via email. The site worked properly until it was migrated recently to an IIS 7.5 Server. I had to recode the portion that does the file uploading in ASP.NET with C# code behind and am having trouble getting a String passed from new to old.</p>
  
  <p>Previously, the .ASP code use the ASPupload component which, for sound reasons, is no longer supported on the new hosting Server. ASPupload used to pass the two uploaded filenames (via JavaScript as a Hidden Input value) to a section of the .ASP code that uses those filenames as a String (plus the path String that it already has). Then the .ASP code attaches the files to an email sent to the company fulfilling the work order.</p>
  
  <p>I would like to take the new C# Global String Variable and pass it to the old .ASP code that does the emailing, since it still works like a charm.</p>
  
  <p>I have evolved the Code Behind and ASP.NET on this question to reflect <a href="https://stackoverflow.com/users/1222596/manoj-kumar-sharma" title="Manoj Kumar Sharma&#39;s">Manoj Kumar Sharma's</a> excellent JavaScript that sends the C# Global String Variables to the ASP.NET code as a JavaScript Variable, which I was having trouble with initially. THANKS!!! And now we need to bridge the gap between the new ASP.NET code and the old .ASP code.</p>
  
  <p>Here is the C# Code Behind (thanks for the edits <a href="https://stackoverflow.com/users/1139830/mason" title="Mason">Mason</a>, it is now much easier to update this post): </p>
  
  <pre><code>// From https://support.microsoft.com/en-us/kb/323246
  using System;
  using System.Collections;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Web;
  using System.Web.SessionState;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.HtmlControls;
  
  namespace CSharpUpload
  {
      /// &lt;summary&gt;
      /// Browse to two files, upload to path on Server. Check if files haven't been downloaded, pass name.
      /// &lt;/summary&gt;
  
      public class Scott_upload : System.Web.UI.Page
      {
          protected System.Web.UI.HtmlControls.HtmlInputFile File_Upload_1;
          protected System.Web.UI.HtmlControls.HtmlInputFile File_Upload_2;
          protected System.Web.UI.HtmlControls.HtmlInputButton Submit_Upload;
  
          public string UpFileName1 { get; set; }
          public string UpFileName2 { get; set; }
          public int File_Count;
  
          public bool Dont_show_buttons;
  
          private void Page_Load(object sender, System.EventArgs e)
          {
              // Put user code to initialize the page here
  
          }
  
          #region Web Form Designer generated code
          override protected void OnInit(EventArgs e)
          {
              // 
              // CODEGEN: This call is required by the ASP.NET Web Form Designer.
              // 
              InitializeComponent();
              base.OnInit(e);
          }
  
          /// &lt;summary&gt;
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// &lt;/summary&gt;
          private void InitializeComponent()
          {    
              this.Submit_Upload.ServerClick += new System.EventHandler(this.Submit_Upload_ServerClick);
              this.Load += new System.EventHandler(this.Page_Load);
          }
          #endregion
  
          private void Submit_Upload_ServerClick(object sender, System.EventArgs e)
          {
              bool no_file_selected = true;
  
              File_Count = 0;
  
              if( ( File_Upload_1.PostedFile != null ) &amp;&amp; ( File_Upload_1.PostedFile.ContentLength &gt; 0 ) )
              {
                  string fn_1 = System.IO.Path.GetFileName(File_Upload_1.PostedFile.FileName);
                  string server_path_string_1 = @"Data\";
                  string SaveLocation_1 = Server.MapPath(server_path_string_1) + fn_1;
                  try
                  {
                      File_Upload_1.PostedFile.SaveAs(SaveLocation_1);
                      this.UpFileName1 = fn_1;
                      File_Count = File_Count + 1;
                      Dont_show_buttons = true;
                      no_file_selected = false;
  
                      Response.Write("\nUpload Document 1 \"");
                      Response.Write(fn_1);
                      Response.Write("\" has been SUCCESSFULLY uploaded!\n");
                  }
                  catch ( Exception ex )
                  {
                      Response.Write("Error: " + ex.Message);
                      //Note: Exception.Message returns a detailed message that describes the current exception. 
                      //For security reasons, we do not recommend that you return Exception.Message to end users in 
                      //production environments. It would be better to return a generic error message. 
                  }
              }
  
              if( ( File_Upload_2.PostedFile != null ) &amp;&amp; ( File_Upload_2.PostedFile.ContentLength &gt; 0 ) )
              {
                  string fn_2 = System.IO.Path.GetFileName(File_Upload_2.PostedFile.FileName);
                  string server_path_string_2 = @"Data\";
                  string SaveLocation_2 = Server.MapPath(server_path_string_2) + fn_2;
                  try
                  {
                      File_Upload_2.PostedFile.SaveAs(SaveLocation_2);
                      this.UpFileName2 = fn_2;
                      File_Count = File_Count + 1;
                      Dont_show_buttons = true;
                      no_file_selected = false;
  
                      Response.Write("\nUpload Document 2 \"");
                      Response.Write(fn_2);
                      Response.Write("\" has been SUCCESSFULLY uploaded!\n");
                  }
                  catch ( Exception ex )
                  {
                      Response.Write("Error: " + ex.Message);
                      //Note: Exception.Message returns a detailed message that describes the current exception. 
                      //For security reasons, we do not recommend that you return Exception.Message to end users in 
                      //production environments. It would be better to return a generic error message. 
                  }
              }
  
              if (no_file_selected)
              {
                  Response.Write("\nPLEASE SELECT A FILE TO UPLOAD\n");
              }
          }
      }
  }
  </code></pre>
  
  <p>. Here is the new ASP.NET which can see the filesnames as "upFileName1" and "upFileName2":</p>
  
  <pre><code>&lt;%@ Page language="c#" src="Scott_upload.aspx.cs" AutoEventWireup="false" Inherits="CSharpUpload.Scott_upload"%&gt;
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" &gt;
  
  &lt;HTML&gt;
    &lt;HEAD&gt;
      &lt;title&gt;Scott_upload&lt;/title&gt;
      &lt;meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"&gt;
      &lt;meta name="CODE_LANGUAGE" Content="C#"&gt;
      &lt;meta name=vs_defaultClientScript content="JavaScript"&gt;
      &lt;meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5"&gt;
    &lt;/HEAD&gt;
    &lt;body MS_POSITIONING="GridLayout"&gt;
  
          &lt;form id="Scott_file_upload" method="post" enctype="multipart/form-data" runat="server"&gt;
  
              &lt;% if (Dont_show_buttons == false) { %&gt;
                  &lt;div&gt;
                      Schedule A&amp;B are required to complete the survey within 3 days.
                      &lt;br&gt;&lt;br&gt;
                      Upload Document 1 &lt;INPUT type=file id=File_Upload_1 name=File_Upload_1 runat="server" /&gt;
                      &lt;br&gt;
                  &lt;/div&gt;
                  &lt;div&gt;
                      &lt;br&gt;
                      Upload Document 2 &lt;INPUT type=file id=File_Upload_2 name=File_Upload_2 runat="server" /&gt;
                      &lt;br&gt;
                  &lt;/div&gt;
                  &lt;div&gt;
                      &lt;br&gt;&lt;br&gt;
                      &lt;input type="submit" id="Submit_Upload" value="Click To Upload File(s)" NAME="Submit_Upload" runat="server" /&gt;
                      &lt;br&gt;
                  &lt;/div&gt;
              &lt;% } %&gt;
              &lt;% else { %&gt;
                  &lt;div&gt;
                      &lt;br&gt;&lt;br&gt;
                      Upload(s) COMPLETE.
                      &lt;br&gt;
                  &lt;/div&gt;
              &lt;% } %&gt;
          &lt;/form&gt;
  
  &lt;script type="text/javascript"&gt;
  
      var upFileName1 = '&lt;%= this.UpFileName1 %&gt;';
      var upFileName2 = '&lt;%= this.UpFileName2 %&gt;';
  
      ' The code behind filenames are now appearing in the ASP.NET
      document.write(upFileName1);
      document.write(upFileName2);
  
  &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/HTML&gt;
  </code></pre>
  
  <p>And now for the third requested piece, the old .ASP code. Please forgive it, I did not write it, but must maintain it (I am much more specific with variable names). First is a section that calls "Scott_upload.aspx" from an iframe inside of a order request form (note the old Hidden Input "UploadedFile" that ASPupload used to use):</p>
  
  <pre><code>&lt;html&gt;
  &lt;!-- ... --&gt;
  &lt;body&gt;
  &lt;!-- ... --&gt;
  &lt;table class="tblw" border="0" cellpadding="0" cellspacing="0" align="center" ID="Table5"&gt;
  &lt;tr&gt;
  &lt;form name="frmRequest" method="post" action="format_email.asp"&gt;
  &lt;!-- ... --&gt;
      &lt;table class="tblw" border="0" cellpadding="0" cellspacing="0" align="center" ID="Table13"&gt;
      &lt;tr&gt;
          &lt;input type="hidden" name="UploadedFile1" value="" ID="UploadedFile1id"&gt;
          &lt;input type="hidden" name="UploadedFile2" value="" ID="UploadedFile2id"&gt;
          &lt;td class="container" align="center"&gt;
              &lt;iframe src="Scott_upload.aspx" width=520 height=140 name="upload" scrolling="yes" scrollbars="yes" frameborder="0" border="0" style="border:0px;padding:0px;margin:0px;overflow:visible;"&gt;
              &lt;/iframe&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;/table&gt;
  &lt;!-- ... --&gt;
      &lt;input type="button" name="btn" value="REQUEST ORDER" class="submitbtn" onClick="validateForm();"&gt;
  &lt;/tr&gt;
  &lt;/form&gt;
  &lt;/table&gt;
  
  &lt;% Server.Execute %&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Now, the old uploading code that someone else wrote did this bit of JavaScript manipulation to the Hidden Input Value (inside yet another iframe, inside yet another form, IKR?!) which does NOT work in my .ASPX:</p>
  
  <pre><code>&lt;script language="javascript"&gt;
  
  &lt;% If Count = 1 Or Count = 2 Then %&gt;
  window.parent.document.getElementById("UploadedFile1id").value = document.frmUpload.FileName1.value;
  &lt;% End If %&gt;
  
  &lt;% If Count = 2 Then %&gt;
  window.parent.document.getElementById("UploadedFile2id").value = document.frmUpload.FileName2.value;
  &lt;% End If %&gt;
  
  &lt;/script&gt;
  </code></pre>
  
  <p>Then in "format_email.asp" the filename Strings are assigned with this line:</p>
  
  <pre><code>strUpload1 = Trim(Request.Form("UploadedFile1"))
  strUpload2 = Trim(Request.Form("UploadedFile2"))
  </code></pre>
  
  <p>How do I get "upFileName1" and "upFileName2" from the "Scott_upload.aspx" page to assign to "strUpload1" and "strUpload2" in the old "format_email.asp" page?</p>