我在同一页面上有两个ajaxtoolkit文件ulopads,如
<ajaxToolkit:AjaxFileUpload
id="AjaxFileUpload1"
AllowedFileTypes="jpg,jpeg,gif,png"
OnUploadComplete="ajaxUpload2_OnUploadComplete"
runat="server" />
<ajaxToolkit:AjaxFileUpload
id="ajaxUpload1"
AllowedFileTypes="jpg,jpeg,gif,png"
OnUploadComplete="ajaxUpload1_OnUploadComplete"
runat="server" />
和背后的代码
protected void ajaxUpload2_OnUploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string filePath = "~/Images/" + e.FileName;
filePath = filePath.Split('\\').Last();
Session["img2"] = filePath.ToString();
AjaxFileUpload1.SaveAs(MapPath(filePath));
}
protected void ajaxUpload1_OnUploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string filePath = "~/Images/" + e.FileName;
filePath = filePath.Split('\\').Last();
Session["img1"] = filePath.ToString();
ajaxUpload1.SaveAs(MapPath(filePath));
}
问题是每当我使用它上传AjaxFileUpload1并调用void ajaxUpload2_OnUploadComplete方法但是如果我使用ajaxUpload1,则再次调用方法ajaxUpload2_OnUploadComplete但是不调用方法ajaxUpload1
为什么?
感谢。
答案 0 :(得分:4)
我们昨天遇到了同样的问题,我们在同一页面上发现您不能多个 AjaxFileUpload 实例。
如果查看源代码,您会看到此控件使用 常量GUID 来标识其事件。由于 GUID是常量,所有AjaxFileUpload的实例都使用相同的GUID。 ..
结果:
第一个实例吞下所有事件......
以下是行动中的GUID:
private const string ContextKey = "{DA8BEDC8-B952-4d5d-8CC2-59FE922E2923}";
(...)
if (this.Page.Request.QueryString["contextkey"] == ContextKey && this.Page.Request.Files.Count > 0)
答案 1 :(得分:2)
我们按照以下方式定制了2012年9月的工具包 - 希望这是一个临时的解决方法,并在未来的版本中修复:
OLD
private const string ContextKey = "{DA8BEDC8-B952-4d5d-8CC2-59FE922E2923}";
NEW
private string ContextKey = "";
OLD
public AjaxFileUpload()
: base(true, HtmlTextWriterTag.Div)
{
}
NEW
public AjaxFileUpload()
: base(true, HtmlTextWriterTag.Div)
{
if (HttpContext.Current.Items["lastAjaxFileUploadContextKey"] == null)
{
HttpContext.Current.Items["lastAjaxFileUploadContextKey"] = 1;
}
else
{
HttpContext.Current.Items["lastAjaxFileUploadContextKey"] = (int)HttpContext.Current.Items["lastAjaxFileUploadContextKey"] + 1;
}
ContextKey = HttpContext.Current.Items["lastAjaxFileUploadContextKey"].ToString();
}
答案 2 :(得分:1)
实际上有一种方法可以在一个页面上使用多个AjaxFileUpload控件,每个控件都触发自己的事件。解决方案非常简单;它涉及覆盖Microsoft的一个客户端函数,用于AjaxFileUpload控件,以注入实际导致上传完成事件的控件的信息,然后使用单个事件处理程序将所有AjaxFileUpload控件作为“交换机”,随后将触发创建事件服务器端的控件的正确事件处理程序。
以下是如何操作:
在页面的head元素之后的某处添加此脚本块。如果您正在使用母版页,请将其放在HTML内容的占位符中:
<script type="text/javascript">
Sys.Extended.UI.AjaxFileUpload.Control.prototype.doneAndUploadNextFile = function (c) {
var a = new XMLHttpRequest, b = this;
a.open("POST", "?contextKey=" + this._contextKey + "&done=1&guid=" + c._id + "&uplCtrlID=" + b.get_id(), true);
a.onreadystatechange = function () {
if (a.readyState == 4) if (a.status == 200) {
b.raiseUploadComplete(Sys.Serialization.JavaScriptSerializer.deserialize(a.responseText));
b._processor.startUpload()
}
else {
b.setFileStatus(c, "error", Sys.Extended.UI.Resources.AjaxFileUpload_error);
b.raiseUploadError(a);
throw "error raising upload complete event and start new upload";
}
};
a.send(null);
}
</script>
此代码与用于调用页面并触发UploadComplete事件的功能相同,仅修改为添加额外参数 - uplCtrlID - 其中包含真正导致事件的控件的ID。
设置服务器端代码,如下所示:
//set the OnUploadComplete property on all of your AjaxFileUpload controls to this method
protected void anyUploader_UploadComplete(object sender, AjaxFileUploadEventArgs e)
{
//call the correct upload complete handler if possible
if (Request.QueryString["uplCtrlID"] != null)
{
//uplCtrlID (the query string param we injected with the overriden JS function)
//contains the ID of the uploader.
//We'll use that to fire the appropriate event handler...
if (Request.QueryString["uplCtrlID"] == FileUploaderA.ClientID)
FileUploaderA_UploadComplete(FileUploaderA, e);
else if (Request.QueryString["uplCtrlID"] == FileUploaderB.ClientID)
FileUploaderB_UploadComplete(FileUploaderB, e);
//etc (or use a switch block - whatever suits you)
}
}
protected void FileUploaderA_UploadComplete(AjaxFileUpload sender, AjaxFileUploadEventArgs e)
{
//logic here
}
protected void FileUploaderB_UploadComplete(AjaxFileUpload sender, AjaxFileUploadEventArgs e)
{
//logic here
}
你们都准备好了。多个AjaxFileUpload控件在同一页面上,没有问题。