Now we can access these attributes using code on the client to see how they work. Clear all files in the virtual directory, create a new app_code folder, create a "simplecontrol. cs" file in it, and enter the following code:
Example 6-1 code 1: simplecontrol. CS code
Using system;
Using system. Web. UI;
Namespace mycontrol
{
Public class linkscontrol: Control
{
Protected int _ num = 0;
Public Virtual int num
{
Get {return _ num ;}
Set {_ num = value ;}
}
Protected override void render (htmltextwriter writer)
{
Writer. Write (Num. tostring ());
}
}
}
Here we create a very simple control, which has an integer property num, but simply outputs the value of this property in render. Next we will access this attribute value in the aspx file. Create a simple. aspx file in the virtual directory and enter the following code:
Example 6-1 Code 2: simple. aspx code
<SCRIPT runat = "server" Language = "C #">
Void button#click (Object sender, eventargs E)
{
LC. Num ++;
}
</SCRIPT>
<% @ Register tagprefix = "CG" namespace = "mycontrol" %>
<HTML>
<Body>
<Form runat = "server">
<CG: linkscontrol id = "LC" runat = "server"/>
<Asp: button id = "button1" runat = "server" onclick = "button#click"
TEXT = "button"/>
</Form>
</Body>
</Html>
In the aspx file, add a button control and Add 1 to the num attribute of the control we just customized in its click event. Run the simple. aspx file in the browser. At this time, only one digit 0 and one button appear. We click the button multiple times, but the number is always 1, which is not as increasing as we expected. Why?
In fact, we should all know that the client sends a request to the server, and the server no longer maintains the client information after the server responds. In other words, we enter a URL in the browser to send a request to the server. The server responds and instantiate the linkscontrol class. After the response information is returned to the client, the control is released. At this time, we click the button on the webpage to send a request to the server again, and the server will instantiate the linkscontrol class again to respond. That is to say, each time you click a button, all operations are performed on different instances of the linkscontrol class, and the attribute values are normal if they are not saved. WEB programming and winform programming models are a big difference and a headache.
How can this problem be solved? When ASP. NET returns the information to the server, it submits a hidden viewstate control, which stores the status of the control on the page. That is to say, when you click the button, the num attribute of the current linkscontrol object is returned to the server, and the server generates a new page based on this value.
How to obtain the return value? The control class morning has a viewstate attribute, which is a set. You can use viewstate ["num"] to obtain the return value of num. Okay. Open the simplecontrol. CS file and slightly modify the attribute Code as follows:
Example 6-2 code 1: simplecontrol. CS code
Using system;
Using system. Web. UI;
Namespace mycontrol
{
Public class linkscontrol: Control
{
Public Virtual int num
{
Get
{
Return viewstate ["num"]! = NULL?
(INT) viewstate ["num"]: 0;
}
Set
{
Viewstate ["num"] = value;
}
}
Protected override void render (htmltextwriter writer)
{
Writer. Write (Num. tostring ());
}
}
}
Now run simple. aspx and click the button multiple times to see if the number has increased as the number of clicks increases?