我在处理这个问题时找到了一个可以接受的解决方案。从那以后,我收到了来自有同样问题的人的电子邮件,并一直在询问我是否找到了解决方案。所以我在这里展示它,去掉任何多余的代码:
从具有 FileUpload 控件的用户控件中,我首先在 FileUploadComplete 处理程序的背面设置会话变量:
*在 ascx 文件 (upload_chart.ascx) 我有 AsyncFileUpload,重要的是 OnUploadComplete 和 OnClientUploadComplete:*
<ajaxToolkit:AsyncFileUpload
OnUploadedComplete="FileUploadComplete1"
OnClientUploadComplete="UploadComplete1"
ID="ImageFileUploader"
runat="server" />
*在 ascx 文件 (upload_chart.ascx.cs) 后面的代码中,我处理 FileUploadComplete:*
public void FileUploadComplete1(object sender, EventArgs e)
{
try
{
if (ImageFileUploader.FileBytes.Length > 0)
{
// File data is in ImageFileUploaded.FileBytes
// Save it however you need to
// I saved it to a database, in a DBImage Object class I created
// DBImage is specific to my application
ODS.Entity.DBImage pimg =
ODS.Data.DataRepository.SaveImageBytes(ImageFileUploaded.FileBytes);
// Set the ImageID1 in the session
Session["ImageID1"] = pimg.IdImageGroup.ToString();
}
else
{
// error handling for an empty file, however you want to handle it
}
}
catch (Exception Ex)
{
// error handling for an unhandled exception, whatever you want to do here
}
}
Javascript 和脚本方法用于设置页面上的值,这是我对脚本方法的代码隐藏:
// on the aspx page code behind (chartofthedayadmin.aspx.cs) I have the webmethod:
[System.Web.Services.WebMethod]
public static string GetImageID1()
{
System.Web.SessionState.HttpSessionState Session = System.Web.HttpContext.Current.Session;
String retval = Session["ImageID1"].ToString();
Session["ImageID1"] = null;
return retval;
}
这是 javascript: // 在 aspx 前端 (chartofthedayadmin.aspx) 我有 javascript // 来调用 Web 方法和 javascript 失败消息:
function UploadComplete1() {
var str = PageMethods.GetImageID1(uploadSuccess1, uploadFailed);
}
function uploadFailed() {
alert('error occurred or some meaningfull error stuff');
}
*// 用户控件(upload_chart.ascx)上的javascript设置隐藏字段的值*
function uploadSuccess1(result) {
document.getElementById('<%= fldImageID.ClientID %>').value = result;
}
注意:确保您的脚本管理器具有 EnablePageMethods="true"。