The most basic element of a control is the property, which has many types. In this section, I will first introduce the following simple attributes. Let's take a Textbox, the most familiar control, text (string), enabled (bool), and columns (INT) are all attributes. These are simple type attributes that I will talk about today.
Attribute writing is very simple. General attributes can be read/write-only or read-only. These attributes depend on the "accesser" of the attribute. The general writing method is as follows:
Class Pro
{
Private String _ Readonlypro = " I'm readonly! " ;
Private String _ Writeonlypro;
Private String _ Readandwritepro;
/// <Summary>
/// Read-only attribute
/// </Summary>
Public String Readonlypro
{
Get { Return _ Readonlypro ;}
}
/// <Summary>
/// Write only attributes
/// </Summary>
Public String Writeonlypro
{
Set {_ Writeonlypro = Value ;}
}
/// <Summary>
/// Read/write attributes
/// </Summary>
Public String Readandwritepro
{
Get { Return _ Readandwritepro ;}
Set {_ Readandwritepro = Value ;}
}
}
AboveCodeAdd a class3Attribute (read-only, write-only, and read/write.
But what I mentioned in section 2 of this seriesWebProgramDesign is to consider inWebThe request duration is not longer than the request duration. In this case, we cannot use simple private variables to store attribute values.Viewsate, The following example shows how to useViewstateAnd uselessViewsatsTo save the difference between attributes.
First, define an inherited fromControlClass, which has an attribute that is usedViewstateWhile the other is saved with a private variable.
Public Class Viewstatecontrol: Control
{
/// <Summary>
/// Viewstate attributes are used.
/// </Summary>
Public String Viewstatepro
{
Get
{
Object OBJ = This . Viewstate [ " Viewstatepro " ];
If (OBJ ! = Null )
{
Return ( String ) OBJ;
}
Return "" ;
}
Set
{
This . Viewstate [ " Viewstatepro " ] = Value;
}
}
Private String _ Nonviewstatepro;
/// <Summary>
/// Viewstate attribute not used
/// </Summary>
Public String Nonviewstatepro
{
Get
{
Return _ Nonviewstatepro;
}
Set
{
_ Nonviewstatepro = Value;
}
}
/// <Summary>
/// Obtains the attribute value.
/// </Summary>
Public String Provalues ()
{
Return " Viewstatepro's value is' " + This . Viewstatepro + " '<Br/> nonviewstatepro's value is' " + This . Nonviewstatepro + " ' " ;
}
}
Next, I will create a web page for testing and addViewsatecontrolAfter referencing the Assembly where the class is located, design the following on the webpage:
< C0: viewstatecontrol ID = "Viewstatecontrol1" Runat = "Server" />
< ASP: button ID = "Btnsetvalue" Runat = "Server" Text = "Setvalue" Onclick = "Btnsetvalue_click" />
FinallyCodebehindOfCSIn the codePageloadEvent2Attribute values, inButtonOfClickShow them in the event
Protected Void Page_load ( Object Sender, eventargs E)
{
If ( ! Ispostback)
{
This . Viewstatecontrol1.viewstatepro = " Hello " ;
This . Viewstatecontrol1.nonviewstatepro = " World " ;
}
}
protected void btnsetvalue_click ( Object sender, eventargs e)
{< br> response. write ( This . viewstatecontrol1.provalues ();
}< br>
After running the program, pressSetvalueOfButtonThe value of viewstatepro is displayed, but the value of nonviewstatepro is not displayed,This shows thatViewstateproInPageloadWhen assigning values inViewstateStored, when you pressButtonBack PagePostBackThis existsViewstateThe value inPostBackThenPostBackThe previous value is assigned to the attribute again.ViewstateproAndNonviewsateproIs a private variable, which is not stored inViewstate, So when you pressButtonBack PagePostBackThe private variable value is destroyed along with the lifecycle of the page.