不调用Ajax工具包文件上载
我在同一页面上有两个ajaxtoolkit文件ulopads
和代码背后
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
为什么??
谢谢。
我们昨天遇到了同样的问题,我们发现在同一页面上不能有多个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)
我们按如下方式定制了2012年9月的工具包 – 希望这是一个临时的解决方法,并在未来的版本中修复:
旧
private const string ContextKey = "{DA8BEDC8-B952-4d5d-8CC2-59FE922E2923}";
新
private string ContextKey = "";
旧
public AjaxFileUpload() : base(true, HtmlTextWriterTag.Div) { }
新
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(); }
实际上有一种方法可以在一个页面上使用多个AjaxFileUpload控件,每个控件都触发自己的事件。 解决方案非常简单; 它涉及覆盖Microsoft的一个客户端函数,用于AjaxFileUpload控件,以注入实际导致上传完成事件的控件的信息,然后使用单个事件处理程序将所有AjaxFileUpload控件作为“交换机”,随后将触发创建事件服务器端的控件的正确事件处理程序。
这是怎么做的:
在页面的head元素之后的某处添加此脚本块。 如果您正在使用母版页,请将其放在HTML内容的占位符中:
此代码与用于调用页面并触发UploadComplete事件的function相同,仅修改为添加额外参数 – 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控件在同一页面上,没有问题。