Tuesday, January 19, 2010

Problems with AsyncFileUpload because of one Session per Control

I have had the following problem with AsyncFileUpload from Ajax Toolkit:

  • One aspx-Page with one asyncfileupload control
  • Same user could open the page severval times at the same time
  • Problem only one session value for all page instances!

I have found out that the client id was the key to the problem and have inherited from the standard class and have overridden the important properties:

   1: public class ExtendedAsyncFileUpload : AsyncFileUpload
   2:     {
   3:         private string newClientID = null;
   4:         private string newUniqueID = null;
   5:  
   6:  
   7:         /// <summary>
   8:         /// Unique ID for session of a user
   9:         /// </summary>
  10:         protected string SessionUniqueControlIdentifier
  11:         {
  12:             get
  13:             {
  14:                 if (string.IsNullOrEmpty(ViewState["SessionUniqueControlIdentifier"] as string))
  15:                     ViewState["SessionUniqueControlIdentifier"] = Guid.NewGuid().ToString().Replace("-", "");
  16:                 return ViewState["SessionUniqueControlIdentifier"] as string;
  17:             }
  18:         }
  19:  
  20:         protected override void OnPreRender(EventArgs e)
  21:         {
  22:             base.OnPreRender(e);
  23:         }
  24:  
  25:  
  26:         public override string ClientID
  27:         {
  28:             get
  29:             {
  30:  
  31:                 if (string.IsNullOrEmpty(newClientID))
  32:                     newClientID = UniqueID.Replace("$", "_");
  33:                 return newClientID;
  34:             }
  35:         }
  36:  
  37:         public override string UniqueID
  38:         {
  39:             get
  40:             {
  41:                 if (string.IsNullOrEmpty(newUniqueID) && !base.UniqueID.EndsWith(this.SessionUniqueControlIdentifier))
  42:                     newUniqueID = base.UniqueID + this.SessionUniqueControlIdentifier;
  43:                 return newUniqueID;
  44:             }
  45:         }
  46:  
  47:         /// <summary>
  48:         /// Overriden because, original method catches all exceptions, no matter what...
  49:         /// </summary>
  50:         public byte[] FileBytesNew
  51:         {
  52:             get
  53:             {
  54:                 HttpPostedFile file = base.PostedFile;
  55:                 if (file != null)
  56:                     return this.GetBytesFromStream(file.InputStream);
  57:                 else
  58:                     return null;
  59:             }
  60:         }
  61:  
  62:     }

0 comments: