0
我正在創建一個自定義服務器控件。這裏是相關物件自定義控件:爲什麼我的自定義服務器控件不維護視圖狀態信息?
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; }
}
}
我添加的數據源對象代碼的Default.aspx的背後是這樣的:
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();
}
}
第一,它工作正常,但是當我回發頁面控制消失。所以我在Load事件中檢查了Conrols集合,並且看到它是空的。比這更糟糕的是grid.DataSource在回發上爲空! 爲什麼Gridview的視圖狀態不被維護,並且數據源值在回發時丟失?順便說一下,viewstate在page或web.config文件中的任何地方都沒有關閉。
一些資源。 http://support.microsoft.com/kb/893667 http://www.asp.net/web-forms/videos/how-do-i/how-do-i-save-and-load-view-state - 信息換一個定製的Web服務器的控制 – Lloyd 2012-01-08 14:04:45