System. Web. UI is a namespace used to provide basic classes for the development interface elements.
Server controls in C #2.0 all include functions such as data binding and view status. To implement these functions for custom controls, you must use the classes provided by the system. Web. UI namespace. These classes involve attributes, methods, and events of controls.
In system. Web. UI, the two most important classes are "page" and "control. "Page" is the base class of all page-level controls, such as "masterpage", while "control" provides general functions for all server controls.
Class for managing view status: statebag
The statebag class provides the property saving function for controls. The statebag class is a status package that contains a set of attributes of all controls on the page. These collections are accessed in. net in the form of control. viewstate.
For example, how do I save the data that the user has filled in when a textbox returns a page? In this case, statebag is used to save the user input value, that is, to save the view status of the Textbox Control. The view status is tracked after the oninit method on the page. The statebag class stores its internal control view in a dictionary, uses a key/value pair, and then sends it to the browser as a hidden field.
Syntax definition:
Public sealed class statebag: istatemanager, idictionary, icollection, ienumerable
The "istatemanager" interface defines some methods for accessing the view State. The other three interfaces are used to define methods related to the set dictionary. collections "namespace highlights.
The constructor using the statebag class has heavy loads. Its syntax is as follows:
Public statebag (bool ignorecase)
Public statebag ()
A constructor with the ignorecase parameter, indicating that the case sensitivity of stored data can be distinguished. When using the statebag class, this class is usually not instantiated using the new keyword, but directly using the viewstate attribute of the control. The latter is an object of the statebag class.
Attribute details:
The statebag class is a dictionary storage structure, and its attributes are similar to those of other dictionary classes. These attributes and their descriptions are listed below:
Count: Number of stateitem objects
Item: the value of the stored item
Keys: key set of the item
Values: a set of view states.
Detailed solution:
The statebag class method is also the basic method of all dictionary classes. Common ones are listed below:
Add: add an item to statebag
Clear: Clear items in statebag
Isitemdirty: determines whether the items in statebag are marked as modified.
Remove: Remove a specified item.
Setdirty: sets the modified flag of statebag.
Setitemdirty: sets the modified tag of an item in statebag.
The following code demonstrates how to use methods in the statebag class:
Viewstate. add ("textvalue", textbox1.text); // Add the item if (viewstate. isitemdirty ("textvalue") // determines whether to modify viewstate. setitemdirty ("textvalue", true); // set it to modify the viewstate. remove ("textvalue"); // remove the viewstate item. clear (); // clear the view
Typical applications: Save attributes in custom controls
The purpose of this example is to save the control settings when the current user refreshes the interface. Currently, server controls all have this attribute. This example is only used to illustrate the role and principle of the statebag class.
Steps for demo instance:
1. Create a new website named statebagsample ".
2. Add a class named mylabel. CS to the app_code directory.
3. Define a custom control in the class. This control inherits the control class and has only one attribute "text", which is similar to a simple label control. The Code is as follows. Note that the user attributes are not saved.
/// <Summary> /// Summary of mytextbox /// </Summary> namespace mylabel {public class mylabel: Control {private string text; public String text {get {return this. text;} set {This. TEXT = value;} // get {return viewstate ["text"]. tostring () ;}// set {viewstate ["text"] = value ;}} public mylabel () {}protected override void render (htmltextwriter writer) {writer. write (this. text );}}}
4. Open the default. ASPX page and register the defined control in the source code view. The Code is as follows:
<% @ Register tagprefix = "mylbl" namespace = "mylabel" %>
5. In the source code view, add a button to refresh the page. The code for adding a custom control is as follows:
<mylbl:MyLabel ID="lbl1" runat="server" Text=""></mylbl:MyLabel>
6. Enter the test code in the page_load event as follows:
If (! This. ispostback)
Lbl1.text = "this is a test ";
7. Run the program. You can see the displayed content of the custom control. Then, click the button to see that the content of the custom control disappears. This indicates that the view status is not saved.
8. Modify the attributes of the custom control class "mylabel. cs" as follows. Repeat the test in Step 7 to test whether the control status is properly saved.
Get {return viewstate ["text"]. tostring ();}
Set {viewstate ["text"] = value ;}
Result: The control status is saved normally.