2015-05-14 53 views
0

我用RadPersistenceManager用asp.net和遵循以下指導:Telerik的RadPersistenceManager無法讀取存儲內容

http://demos.telerik.com/aspnet-ajax/persistence-framework/examples/custom-storage-provider/defaultcs.aspx

但是,當我實現這個在我的項目出現以下異常:

標題:xxxx.aspx,爲XXXXX方法名稱: 無法讀取存儲的內容。找不到文件 'C:\ inetpub \ wwwroot \ XXXX \ App_Data \ TelerikAspNetRadControlsPersistedState'。

例外:在 Telerik.Web.UI.PersistenceFramework.AppDataStorageProvider.LoadStateFromStorage(字符串 鍵)在Telerik.Web.UI.RadPersistenceManager.LoadState()在GraphicalUserInterface.JobBasket.LoadGridJobBasket()

+0

請發佈您的代碼,這是產生此錯誤。 – SuperBiasedMan

+0

上述指南中的代碼除RadScriptManager位於母版頁中外。 – Yousef

回答

0

的演示和我的示例不同,我需要加載頁面加載時的持久性配置,而不是加載按鈕。所以CookieStorageProvider似乎有時並未在Page_Init中啓動,所以我將它添加到了page_load中。

0

這是默認的存儲提供商密鑰。如果控件正在查找它,那麼您的自定義提供程序完全不起作用。

確保您從演示所擁有的一切,主要是:

是設置自定義存儲提供商的線路:

protected void Page_Init(object sender, EventArgs e) 
{ 
    RadPersistenceManager1.StorageProviderKey = CookieName; 
    RadPersistenceManager1.StorageProvider = new CookieStorageProvider(CookieName); 
} 

和自定義存儲提供商本身:

using System; 
using System.Linq; 
using System.Web; 
using Telerik.Web.UI.PersistenceFramework; 
using System.IO; 
using System.IO.Compression; 
using System.Text; 

public class CookieStorageProvider : IStateStorageProvider 
{ 
    private static readonly Encoding AsciiEncoding = System.Text.Encoding.ASCII; 
    private static readonly int MaxCookieSize = 4000; 
    private static readonly int LengthDataByteCount = sizeof(Int32); 
    private string StorageKey { get; set; } 

    #region IStateStorageProvider 

    public CookieStorageProvider(string key) 
    { 
     StorageKey = key; 
    } 

    public void SaveStateToStorage(string key, string serializedState) 
    { 
     HttpCookie cookie = new HttpCookie(StorageKey); 
     string settingsData = CompressString(serializedState); 

     if (settingsData.Length > MaxCookieSize) 
     { 
      throw new ArgumentOutOfRangeException("Current settings exceed 4k in compressed form! Operation canceled!"); 
     } 

     cookie.Value = settingsData; 

     HttpContext.Current.Response.Cookies.Add(cookie); 
    } 

    public string LoadStateFromStorage(string key) 
    { 
     return DecompressString(HttpContext.Current.Request.Cookies[StorageKey].Value.ToString()); 
    } 

    #endregion 

    private string CompressString(string inputString) 
    { 
     byte[] outputBytes = null; 
     byte[] inputBytes = AsciiEncoding.GetBytes(inputString); 

     using (MemoryStream ms = new MemoryStream()) 
     { 
      using (GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress)) 
      { 
       zipStream.Write(inputBytes, 0, inputBytes.Length); 
      } 
      outputBytes = ms.ToArray(); 
     } 

     return Convert.ToBase64String(AddDataCount(outputBytes, inputBytes.Length)); 
    } 

    private string DecompressString(string inputString) 
    { 
     string outputString = String.Empty; 
     byte[] inputBytes = Convert.FromBase64String(inputString); 
     Int32 lengthDataArray = BitConverter.ToInt32(inputBytes, inputBytes.Length - LengthDataByteCount); 
     byte[] outputBytes = new byte[lengthDataArray]; 

     using (MemoryStream ms = new MemoryStream(RemoveDataCount(inputBytes))) 
     { 
      using (GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress)) 
      { 
       zipStream.Read(outputBytes, 0, outputBytes.Length); 
      } 
      outputString = AsciiEncoding.GetString(outputBytes); 
     } 

     return outputString; 
    } 

    private byte[] AddDataCount(byte[] inputArray, Int32 length) 
    { 
     byte[] lengthDataArray = BitConverter.GetBytes(length); 
     Array.Resize<byte>(ref inputArray, inputArray.Length + LengthDataByteCount); 
     Array.Copy(lengthDataArray, 0, inputArray, inputArray.Length - LengthDataByteCount, LengthDataByteCount); 
     return inputArray; 
    } 

    private byte[] RemoveDataCount(byte[] inputArray) 
    { 
     Array.Resize<byte>(ref inputArray, inputArray.Length - LengthDataByteCount); 
     return inputArray; 
    } 
}