The controls we developed generally need to interact with the programmers who use the controls. There are two ways to interact: one is property and the other is event. Attributes may be simple types such as int and string, but also complex types such as set.
1. Interaction between attributes
The preceding example is used to add the picshow attribute:
Public class picshow: Control
{
Private string _ imgurl;
// Attributes
Public Virtual string imgurl
{
Get
{
Return this. _ imgurl;
}
Set
{
This. _ imgurl = value;
}
}
Protected override void render (htmltextwriter writer)
{
Writer. addstyleattribute (htmltextwriterstyle. textalign, "center ");
Writer. addstyleattribute (htmltextwriterstyle. Height, "100px ");
Writer. addstyleattribute (htmltextwriterstyle. Width, "100px ");
Writer. renderbegintag (htmltextwritertag. Div );
// Create IMG tag
Writer. addattribute (htmltextwriterattribute. SRC, this. imgurl );
Writer. addstyleattribute (htmltextwriterstyle. Width, "80px ");
Writer. addstyleattribute (htmltextwriterstyle. Height, "80px ");
Writer. renderbegintag (htmltextwritertag. IMG );
Writer. renderendtag ();
// End of Div
Writer. renderendtag ();
}
}
In the preceding example, the previous control can only be specified as dead. Image In this way, the image address can be specified by the control user. The control defines a private field and sets its value through the attribute imgurl. Because we allow rewriting of its attribute, it is defined as virtual. The attribute definition here is the same as that of a general class, so we will not explain it much.
Ii. View status
In Project This control is applied, but we find that the status of the properties of this control cannot be maintained. That is to say, it can only maintain the value we initially gave it.
For example:
Http: // www. w3.
Org/TR/xhtml1/dtds/xhtml1-transitional.dtd ">
Http://www.w3.org/1999/xhtml>
Imgurl = "images/1109508867.jpg">
Onclick = "btnchange_click"/>
We use buttons to change its attributes:
Protected void btnchange_click (Object sender, eventargs E)
{
If (this. psdemo. imgurl = "images/1109508867.jpg ")
This. psdemo. imgurl = "images/bg.jpg ";
Else
This. psdemo. imgurl = "images/1109508867.jpg ";
}
After running, we will find that our images can only be changed once. The reason is very simple. After the client sends an HTTP request, the server does not help us maintain the last request information.
In some cases, we must maintain the status. Status can be divided into two types: View status and control status.
The view State is stored in a hidden domain named "_ viewstate" in the script to save the last user request. The view State is a set (viewstate ).
Let's inherit a picshow
public class viewStatePic : PicShow
{
public override string ImgUrl
{
get
{
string strImgUrl = (string)ViewState["ImgUrl"];
return (strImgUrl == null) ? String.Empty : strImgUrl;
}
set
{
ViewState["ImgUrl"] = value;
}
}
}
Here we only rewrite imgurl and implement it through the view State. In this case, we can maintain the previous user request during use.
3. control status
We will find that if you disable the view status (enableviewstate = "false" is added in), we still cannot maintain the user's previous request, in this case, we can maintain the control state forever.
The control status is generally maintained in three steps:
1. Use Page. registerrequirescontrolstate () Method To notify the page where the control is running and maintain the control status.
2. Override the savecontrolstate () method to save the value of the control state.
3. Finally, return the control status value to the property.
Control status example:
Public class controlstatepic: albumn
{
Protected override void oninit (eventargs E)
{
Page. registerrequirescontrolstate (this );
Base. oninit (E );
}
Protected override object savecontrolstate ()
{
Return this. imgurl;
}
// View the saved controls Retrieving Come out
Protected override void loadcontrolstate (Object savedstate)
{
This. imgurl = savedstate as string;
}
}
Note that when we override the oninit method, we just need to add additional Function To register the control status, you must call the base. oninit (e) method to save the previous initialization work.