Use viewstate in Asp.net page to read attributes in user control

Source: Internet
Author: User

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
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.