简体   繁体   English

如何将全局字符串变量从ASP.NET页后面的C#代码传递到经典ASP页?

[英]How to pass a Global String Variable from the C# code behind of an ASP.NET page to a classic ASP page?

All, I am maintaining some old .ASP code for a website that requests a work order via email. 所有人,我正在为通过电子邮件请求工单的网站维护一些旧的.ASP代码。 The site worked properly until it was migrated recently to an IIS 7.5 Server. 该站点正常工作,直到最近将其迁移到IIS 7.5服务器为止。 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. 我必须重新编码在ASP.NET中执行文件上传的部分,后面带有C#代码,并且无法使String从新传递到旧。

Previously, the .ASP code use the ASPupload component which, for sound reasons, is no longer supported on the new hosting Server. 以前,.ASP代码使用ASPupload组件,出于可靠的原因,新的托管服务器不再支持该组件。 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). ASPupload用于将两个上传的文件名(通过JavaScript作为隐藏输入值)传递到.ASP代码的一部分,该部分将这些文件名用作字符串(加上它已经具有的路径String)。 Then the .ASP code attaches the files to an email sent to the company fulfilling the work order. 然后,.ASP代码将文件附加到发送给履行工作订单的公司的电子邮件中。

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. 我想使用新的C#全局字符串变量并将其传递给执行电子邮件发送的旧.ASP代码,因为它仍然像一种魅力一样工作。

I have evolved the Code Behind and ASP.NET on this question to reflect Manoj Kumar Sharma's 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. 我在这个问题上改进了Behind Code和ASP.NET,以反映Manoj Kumar Sharma出色的JavaScript,该JavaScript将C#全局字符串变量作为JavaScript变量发送到ASP.NET代码,而我最初遇到了麻烦。 THANKS!!! 谢谢!!! And now we need to bridge the gap between the new ASP.NET code and the old .ASP code. 现在我们需要弥合新的ASP.NET代码和旧的.ASP代码之间的鸿沟。

Here is the C# Code Behind (thanks for the edits Mason , it is now much easier to update this post): 这是C#代码背后的内容(感谢Mason所做的编辑,现在更容易更新此帖子):

// 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
{
    /// <summary>
    /// Browse to two files, upload to path on Server. Check if files haven't been downloaded, pass name.
    /// </summary>

    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);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        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 ) && ( File_Upload_1.PostedFile.ContentLength > 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 ) && ( File_Upload_2.PostedFile.ContentLength > 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");
            }
        }
    }
}

. Here is the new ASP.NET which can see the filesnames as "upFileName1" and "upFileName2": 这是新的ASP.NET,可以将文件名视为“ upFileName1”和“ upFileName2”:

<%@ Page language="c#" src="Scott_upload.aspx.cs" AutoEventWireup="false" Inherits="CSharpUpload.Scott_upload"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>
  <HEAD>
    <title>Scott_upload</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
  </HEAD>
  <body MS_POSITIONING="GridLayout">

        <form id="Scott_file_upload" method="post" enctype="multipart/form-data" runat="server">

            <% if (Dont_show_buttons == false) { %>
                <div>
                    Schedule A&B are required to complete the survey within 3 days.
                    <br><br>
                    Upload Document 1 <INPUT type=file id=File_Upload_1 name=File_Upload_1 runat="server" />
                    <br>
                </div>
                <div>
                    <br>
                    Upload Document 2 <INPUT type=file id=File_Upload_2 name=File_Upload_2 runat="server" />
                    <br>
                </div>
                <div>
                    <br><br>
                    <input type="submit" id="Submit_Upload" value="Click To Upload File(s)" NAME="Submit_Upload" runat="server" />
                    <br>
                </div>
            <% } %>
            <% else { %>
                <div>
                    <br><br>
                    Upload(s) COMPLETE.
                    <br>
                </div>
            <% } %>
        </form>

<script type="text/javascript">

    var upFileName1 = '<%= this.UpFileName1 %>';
    var upFileName2 = '<%= this.UpFileName2 %>';

    ' The code behind filenames are now appearing in the ASP.NET
    document.write(upFileName1);
    document.write(upFileName2);

</script>

</body>
</HTML>

And now for the third requested piece, the old .ASP code. 现在,第三个请求的部分是旧的.ASP代码。 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): 首先是一个从订单请求表单内的iframe调用“ Scott_upload.aspx”的部分(请注意ASPupload过去使用的旧的隐藏输入“ UploadedFile”):

<html>
<!-- ... -->
<body>
<!-- ... -->
<table class="tblw" border="0" cellpadding="0" cellspacing="0" align="center" ID="Table5">
<tr>
<form name="frmRequest" method="post" action="format_email.asp">
<!-- ... -->
    <table class="tblw" border="0" cellpadding="0" cellspacing="0" align="center" ID="Table13">
    <tr>
        <input type="hidden" name="UploadedFile1" value="" ID="UploadedFile1id">
        <input type="hidden" name="UploadedFile2" value="" ID="UploadedFile2id">
        <td class="container" align="center">
            <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;">
            </iframe>
        </td>
    </tr>
    </table>
<!-- ... -->
    <input type="button" name="btn" value="REQUEST ORDER" class="submitbtn" onClick="validateForm();">
</tr>
</form>
</table>

<% Server.Execute %>

</body>
</html>

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: 现在,别人写的旧的上载代码对隐藏的输入值(在另一个iframe中,在另一个表单IKR中!)进行了JavaScript操作,这在我的.ASPX中不起作用:

<script language="javascript">

<% If Count = 1 Or Count = 2 Then %>
window.parent.document.getElementById("UploadedFile1id").value = document.frmUpload.FileName1.value;
<% End If %>

<% If Count = 2 Then %>
window.parent.document.getElementById("UploadedFile2id").value = document.frmUpload.FileName2.value;
<% End If %>

</script>

Then in "format_email.asp" the filename Strings are assigned with this line: 然后在“ format_email.asp”文件名字符串中分配以下行:

strUpload1 = Trim(Request.Form("UploadedFile1"))
strUpload2 = Trim(Request.Form("UploadedFile2"))

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? 如何从“ Scott_upload.aspx”页面获取“ upFileName1”和“ upFileName2”,以在旧的“ format_email.asp”页面中分配给“ strUpload1”和“ strUpload2”?

You could try this: 您可以尝试以下方法:

Declare the two variables as Properties of the Page (something like this): 将两个变量声明为“页面的属性”(类似这样):

public class Scott_upload : System.Web.UI.Page
{
    public string UpFileName1 { get; set; }
    public string UpFileName2 { get; set; }
...

Then in the .ASPX page, where you have your javascript code, embed the values into javascript variables (something like this): 然后在拥有您的Javascript代码的.ASPX页面中,将值嵌入到javascript变量中(类似这样):

<script type="text/javascript">

    var upFileName1 = '<%= this.UpFileName1 %>';
    var upFileName2 = '<%= this.UpFileName2 %>';

</script>

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM