[英]How to pass a Global String Variable from the C# code behind of an ASP.NET page to a classic ASP page?
所有人,我正在為通過電子郵件請求工單的網站維護一些舊的.ASP代碼。 該站點正常工作,直到最近將其遷移到IIS 7.5服務器為止。 我必須重新編碼在ASP.NET中執行文件上傳的部分,后面帶有C#代碼,並且無法使String從新傳遞到舊。
以前,.ASP代碼使用ASPupload組件,出於可靠的原因,新的托管服務器不再支持該組件。 ASPupload用於將兩個上傳的文件名(通過JavaScript作為隱藏輸入值)傳遞到.ASP代碼的一部分,該部分將這些文件名用作字符串(加上它已經具有的路徑String)。 然后,.ASP代碼將文件附加到發送給履行工作訂單的公司的電子郵件中。
我想使用新的C#全局字符串變量並將其傳遞給執行電子郵件發送的舊.ASP代碼,因為它仍然像一種魅力一樣工作。
我在這個問題上改進了Behind Code和ASP.NET,以反映Manoj Kumar Sharma出色的JavaScript,該JavaScript將C#全局字符串變量作為JavaScript變量發送到ASP.NET代碼,而我最初遇到了麻煩。 謝謝!!! 現在我們需要彌合新的ASP.NET代碼和舊的.ASP代碼之間的鴻溝。
這是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");
}
}
}
}
。 這是新的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>
現在,第三個請求的部分是舊的.ASP代碼。 請原諒,我沒有寫它,但是必須維護它(我對變量名更加具體)。 首先是一個從訂單請求表單內的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>
現在,別人寫的舊的上載代碼對隱藏的輸入值(在另一個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>
然后在“ format_email.asp”文件名字符串中分配以下行:
strUpload1 = Trim(Request.Form("UploadedFile1"))
strUpload2 = Trim(Request.Form("UploadedFile2"))
如何從“ Scott_upload.aspx”頁面獲取“ upFileName1”和“ upFileName2”,以在舊的“ format_email.asp”頁面中分配給“ strUpload1”和“ strUpload2”?
您可以嘗試以下方法:
將兩個變量聲明為“頁面的屬性”(類似這樣):
public class Scott_upload : System.Web.UI.Page
{
public string UpFileName1 { get; set; }
public string UpFileName2 { get; set; }
...
然后在擁有您的Javascript代碼的.ASPX頁面中,將值嵌入到javascript變量中(類似這樣):
<script type="text/javascript">
var upFileName1 = '<%= this.UpFileName1 %>';
var upFileName2 = '<%= this.UpFileName2 %>';
</script>
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.