Asp.net defines attributes persistence and how to dynamically load user controls.

Source: Internet
Author: User

1. Property persistence of User Controls: (reference: Not far away-in-depth analysis of ASP. net2.0 control development)

Problem description:After assigning values to user control attributes, refresh the page or return the page to the server, the attributes of the user control will disappear, but the built-in control of. NET will not have this problem.

Cause:To explain the problem above, we need to review the working mode of the HTTP protocol. HTTP is a stateless disconnected connection mode, that is, the client sends a request to the server, after the server responds, the request client information is not maintained. By default, multiple requests come from the same client or multiple different clients. The processing method is no different for the server.

Therefore, after we click the button on the page to upload the page back to the server, the server cannot restore all the status of the control in the page to the value set in the last request, because it does not know the last request, nor does it save historical information for each request.

Solution:

This stateless feature of HTTP brings about inconsistency with the winforms development model for our development, and the interaction results between users and controls cannot be saved.

In the actual development process, we found ASP. The control provided by. Net can store its own status during page return. For example, we add a label control on the page and add it to the button event:

This. lblhappy. Text + = This. lblhappy. Text + "again"

The text displayed by the label can be correctly added with "again" based on the value set in the previous request. What is the problem?

ASP. NET introduced the viewstate feature in order to solve the contradiction between the control state storage. You need to save the value control attribute during page return. You can save the value in view status. (1) Asp. net Framework will serialize the view status information into a string before rendering the page (the server sends the page to the client, and save it to a hidden form field named "_ viewstate" on the page (<input type = 'den den '>. in this way, the control status is saved to the client. (2) When the form field is returned next time (the client submits the page to the server), the server deserializes the value of the _ viewstate hidden field submitted back to restore the status of each control.

You can also explain the principle of viewstate in this way: when requesting a page, ASP. NET serializes the status of all controls into a string and sends it to the client as a hidden property of the form. When the client transfers the page back, ASP. NET analyzes the form attributes returned by the return and assigns them to the corresponding values of the control. Of course, all of these are the responsibility of ASP. NET.

Example:

Code
1 public class albumnusingviewstate: albumn
2 {
3 Public override string imgurl
4 {
5 get
6 {
7 object o = viewsate ["imgurl"];
8 If (o! = NULL)
9 {
10 return (string) O;
11}
12 Return string. empty;
13
14}
15 set
16 {
17 viewsate ["imgurl"] = value;
18}
19}
20}

 

2. dynamically reference user controls:

It is very easy to reference a space by dragging the space directly on the page. to dynamically generate a space in the code, use the following method:

(Control class) name = (control class) This. loadcontrol ("... ascx") only requires the control class name and path to define a control. Example: Code
1 business_usercontrol_usercheckinfo usercheckinfo1 = (business_usercontrol_usercheckinfo) loadcontrol (@ "usercontrol \ usercheckinfo. ascx ");
2 usercheckinfo1.uidandtype = "Number ";
3 panel1.controls. Add (usercheckinfo3); the first line is to dynamically generate a control, the second line is to assign values to the control attributes, and the third line is to put the control in a container to display the control. Conclusion: Dynamic Control loading is too troublesome and involves many internal principles. It is better to directly throw the user control into the template of the gridview, and assign a value to the attribute for initialization.. In this way, let. Net Save the user control information, so we don't have to worry about it.

 

 

 

 

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.