我有一个带有GridView的应用程序,这个GridView在你的PagerTemplate中有一个GridViewPager,它是一个UserControl。
现在我正在尝试将此GridViewPager存储在ViewState中,并且我遇到了序列化问题。 Asp.Net告诉我该类没有标记为可序列化。我尝试将其标记为可序列化,如下所示:
namespace Avalon.View.UserControls.Pagers
{
[Serializable]
public partial class GridViewPager : System.Web.UI.UserControl
{
private GridView _gridView;
public event EventHandler OnTextPageChanged;
// Methods, properties...
}
}
但它不起作用
我的代码很简单,让我们看看:
// Get and set the GridViewPager to ViewState
public partial class ChamadoList : System.Web.UI.UserControl
{
// Here a Get and set my GridViewPager into ViewState
public GridViewPager gvp
{
get { return ((GridViewPager)ViewState["GridViewPager"]); }
set
{
if (value == null)
ViewState["GridViewPager"] = null;
else
ViewState["GridViewPager"] = value;
}
}
// Looping in a grid View i get the GridViewPager and put it on ViewState
protected void gvListaChamados_RowDataBound(object sender, GridViewRowEventArgs e)
{
// Here a get the pager
if (e.Row.RowType == DataControlRowType.Pager)
{
gvp = (GridViewPager)e.Row.FindControl("GridViewPager1");
}
}
//...
}
这是错误
2>'/'应用程序中的服务器错误。在程序集'App_Web_epwoiz7x中输入'ASP.view_usercontrols_pagers_gridviewpager_ascx',Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null'未标记为可序列化。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.Runtime.Serialization.SerializationException:在程序集'App_Web_epwoiz7x中输入'ASP.view_usercontrols_pagers_gridviewpager_ascx',Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null'未标记为可序列化。
来源错误:
在执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。
堆栈追踪:
[SerializationException:在程序集'App_Web_epwoiz7x中输入'ASP.view_usercontrols_pagers_gridviewpager_ascx',Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null'未标记为可序列化。 System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)+7733643 System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type,StreamingContext context)+258 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()+111 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj,ISurrogateSelector surrogateSelector,StreamingContext context,SerObjectInfoInit serObjectInfoInit,IFormatterConverter converter,ObjectWriter objectWriter)+161 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj,ISurrogateSelector surrogateSelector,StreamingContext context,SerObjectInfoInit serObjectInfoInit,IFormatterConverter converter,ObjectWriter objectWriter)+51 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph,Header [] inHeaders,__BinaryWriter serWriter,Boolean fCheck)+410 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream,Object graph,Header [] headers,Boolean fCheck)+134 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream,Object graph)+13 System.Web.UI.ObjectStateFormatter.SerializeValue(SerializerBinaryWriter writer,Object value)+845
[ArgumentException:序列化'ASP.view_usercontrols_pagers_gridviewpager_ascx'类型的值'ASP.view_usercontrols_pagers_gridviewpager_ascx'时出错。 System.Web.UI.ObjectStateFormatter.SerializeValue(SerializerBinaryWriter writer,Object value)+3395 System.Web.UI.ObjectStateFormatter.Serialize(Stream outputStream,Object stateGraph)+110 System.Web.UI.ObjectStateFormatter.Serialize(Object stateGraph)+57 System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Serialize(Object state)+4 System.Web.UI.Util.SerializeWithAssert(IStateFormatter formatter,Object stateGraph)+37 System.Web.UI.HiddenFieldPageStatePersister.Save()+79 System.Web.UI.Page.SavePageStateToPersistenceMedium(Object state)+105 System.Web.UI.Page.SaveAllState()+236 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+1099
版本信息:Microsoft .NET Framework版本:2.0.50727.3082; ASP.NET版本:2.0.50727.4028
答案 0 :(得分:1)
为什么不让UserControl管理自己的ViewState而不是尝试自行序列化?
public partial class GridViewPager : System.Web.UI.UserControl
{
private int _startPage;
public int StartPage
{
get { return _startPage; }
set { _startPage = value; }
}
protected override void LoadViewState(object savedState)
{
object[] totalState = null;
if (savedState != null)
{
totalState = (object[])savedState;
if (totalState.Length == 2)
{
base.LoadViewState(totalState[0]);
// Load back your properties, etc. from ViewState
if (totalState[1] != null)
startPage = int.Parse(totalState[1]);
}
}
}
protected override object SaveViewState()
{
object baseState = base.SaveViewState();
object[] totalState = new object[2];
// Save out your properties, etc. to ViewState.
totalState[0] = baseState;
totalState[1] = _startPage;
return totalState;
}
}
这样,当您将UserControl添加到页面时,它将管理自己的ViewSate。