用户控制错误原因何在? [英] error in user control why?

查看:69
本文介绍了用户控制错误原因何在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入'<> f__AnonymousTypee`3 [[System.Int64,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.String,mscorlib,Version = 4.0.0.0,Culture =中性,PublicKeyToken = b77a5c561934e089],[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]'在Assembly'App_Web_4prx32q4,Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null'是没有标记为可序列化。





这是我的用户控制代码。在第一行if(ViewState [_lstDataSource] == null )

它会产生上述错误原因??





 < span class =code-keyword> protected   void  Page_Load( object  sender,EventArgs e )
{
尝试
{

if (ViewState [_lstDataSource ] == null)
ViewState [_lstDataSource
] = null ;

}
catch (例外情况)
{
抛出 ex;
}

}
public IEnumerable< Object> getDataSource
{
get
{
return ( IEnumerable< Object>)(ViewState [ _ lstDataSource]);
}
set
{
ViewState [ _ lstDataSource] = value .ToList();

}
}





我在.aspx页面中调用了这个用户控件

 btnExport.getDataSource = query; 
btnExport.Export(query);

解决方案

将以下属性添加到您的财产

< pre lang =c#> [可浏览( false )]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden) ]
public IEnumerable< object> getDataSource
{
get
{
return ( IEnumerable< object>)(ViewState [ _ lstDataSource]);
}
set
{
ViewState [ _ lstDataSource] = value .ToList();

}
}


1。这是一个完全错误的代码:

 尝试 
{

if (ViewState [_lstDataSource ] == null)
ViewState [_lstDataSource
] = null ;

}
catch (例外情况)
{
抛出 ex;
}



为什么?如果 ViewState [_ lstDataSource] 已经 null ,则无需将其设置为null。并且 catch(Exception ex){throw ex; } 没有任何其他东西只是破坏原始异常的callstack信息,它不会添加任何信息或提供任何帮助。



2。问题的根源在于其他地方。 _lstDataSource 的类型在哪里定义?在那里你必须添加一个属性,例如

 [Serializable] 
public class SomeType


Type '<>f__AnonymousTypee`3[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' in Assembly 'App_Web_4prx32q4, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.


This is my code of user control .In first Line if (ViewState[_lstDataSource"] == null)
it generates the above error why??


protected void Page_Load(object sender, EventArgs e)
 {
     try
     {

         if (ViewState[_lstDataSource"] == null)
             ViewState[_lstDataSource"] = null;

     }
     catch (Exception ex)
     {
         throw ex;
     }

 }
 public IEnumerable<Object> getDataSource
 {
     get
     {
         return (IEnumerable<Object>)(ViewState["_lstDataSource"]);
     }
     set
     {
         ViewState["_lstDataSource"] = value.ToList();

     }
 }



I called this user control in my .aspx page

btnExport.getDataSource =query;
               btnExport.Export(query);

解决方案

add below attributes to your property

[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IEnumerable<object> getDataSource
{
    get
    {
       return (IEnumerable<object>)(ViewState["_lstDataSource"]);
    }
    set
    {
       ViewState["_lstDataSource"] = value.ToList();
    
    }
}


1. That's a totally wrong code:

try
{

    if (ViewState[_lstDataSource"] == null)
        ViewState[_lstDataSource"] = null;

}
catch (Exception ex)
{
    throw ex;
}


Why? If ViewState["_lstDataSource"] is already null, there is no need to set it to null. And a catch (Exception ex) { throw ex; } without anything else just destroys the callstack information of the original exception, it does not add any information or offer any help.

2. The source fo your problem is somewhere else. Where is the type of _lstDataSource defined? There you have to add an attribute, something like

[Serializable]
public class SomeType


这篇关于用户控制错误原因何在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆