Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的自定义服务器控件不维护viewstate信息?_C#_Asp.net_.net_Custom Controls - Fatal编程技术网

C# 为什么我的自定义服务器控件不维护viewstate信息?

C# 为什么我的自定义服务器控件不维护viewstate信息?,c#,asp.net,.net,custom-controls,C#,Asp.net,.net,Custom Controls,我正在创建一个自定义服务器控件。以下是自定义控件的相关部分: public class ManagementUserControl : UserControl { GridView _grv; public ManagementUserControl() { _grv = new GridView(); } /// <summary> /// b

我正在创建一个自定义服务器控件。以下是自定义控件的相关部分:

    public class ManagementUserControl : UserControl
    {
        GridView _grv;

        public ManagementUserControl()
        {
            _grv = new GridView();
        }

        /// <summary>
        /// binds the grid to controls.
        /// </summary>
        public override void DataBind()
        {
            _grv.DataBind();
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack)
            {
    //add controls only when is not postback
                InitializeGrid(); 
            }
        }
        void InitializeGrid()
        {
            this.Controls.Add(_grv);
        }
        [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
        public object DataSource
        {
            get { return _grv.DataSource; }
            set { _grv.DataSource = value; }
        }
    }
公共类ManagementUserControl:UserControl
{
GridView_grv;
公共管理用户控制()
{
_grv=新的GridView();
}
/// 
///将网格绑定到控件。
/// 
公共覆盖无效数据绑定()
{
_grv.DataBind();
}
受保护的覆盖无效加载(事件参数e)
{
基础荷载(e);
如果(!IsPostBack)
{
//仅当未回发时添加控件
InitializeGrid();
}
}
void InitializeGrid()
{
this.Controls.Add(_grv);
}
[可浏览(true)、可编辑可浏览(editorbrowsebleState.Always)]
公共对象数据源
{
获取{return\u grv.DataSource;}
设置{u grv.DataSource=value;}
}
}
我在Default.aspx的代码隐藏中添加datasource对象,如下所示:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<string> lst = new List<string>();
            lst.Add("test1");
            lst.Add("test2");
            lst.Add("test3");
            ucManagement.DataSource = lst;
            ucManagement.DataBind();
        }
    }
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
{
List lst=新列表();
第1条添加(“测试1”);
第1条添加(“测试2”);
第1条添加(“测试3”);
ucManagement.DataSource=lst;
ucManagement.DataBind();
}
}
首先,它可以正常工作,但是当我回发页面时,控件就消失了。所以我在Load事件中检查了Conrols集合,发现它是空的。更糟糕的是,grid.DataSource在回发时为null!! 为什么不维护gridview的viewstate,并且回发时数据源值丢失?顺便说一下,在页面或web.config文件中的任何位置都不会关闭viewstate。

可在asp.net网站上找到。似乎正是您的情况。

一些资源。