From: http://www.cxyclub.cn/post/page-ViewState-UserContorl-GetValue.html
User Control
Class Name: webusercontrol
Attribute: Private datatable DT // public protected private can be used here. The key is that viewstate is protected. Therefore, you need to define the following method to read or access
Method:
Private able _ mydt = NULL;
Public datatable mydt
{
Get {
Return (datatable) viewstate ["thedt"]; // Note: you cannot return _ mydt because it cannot save the changed value, that is, assign a value to _ mydt again in the control, the result obtained in the page is null.
}
Set {
_ Mydt = value;
}
}
Event:
Protected void button#click (Object sender, eventargs E)
{
Dataset DS = new dataset ();
......
Dt = Ds. Tables [0];
Viewstate ["thedt"] = DT; // store the value in viewstate in a way similar to session
}
Page
User Control ID: webusercontrol1
Read properties from the control:
Datatable dt = new datatable ();
Protected void button#click (Object sender, eventargs E)
{
Dt = webusercontrol1.mydt; // read
}