IT restaurant-22nd back controls

Source: Internet
Author: User

After lunch on Wednesday, Yu Chen came back from the canteen and found that a colleague surnamed Huang (in the group, he is called la) in the development team reading a copy. net books, because in the past few years, yuchen has basically stopped buying technical books, because the books that really read the eye, valuable, and valuable have basically been repeated in the past few years. Yu Chen walked over with curiosity and asked Lao Huang about the book, because he had passed the so-called "entry", "proficient", and "easy to understand ', for books like 'biaodian', I went straight into the cold season and asked Lao Huang what he was reading.

Lao Huang was also very pleased to hear from others and said, "I am currently reading about control development. This book uses nearly 50 pages to introduce the principle and development process of controls, related function methods, for example."

Yu Chen smiled and said to him, "as I know, in. in net, controls are divided into custom controls and user controls. I have been using user controls for a long time and have been using them for years, especially web user controls. Web custom controls are designed and used only after they are added to the product group. However, I have some experiences over the years, but I don't know if Brother Huang is interested ."

Lao Huang nodded: "Of course, I have also developed and designed some user controls in my previous company, and it is quite easy to use ."

Yu Chen patted Lao Huang on the shoulder and said with a smile: "Indeed, in fact, I feel that there are several advantages of controls. The first is to improve the reusability of code, this encapsulates reusable business logic (mainly reflected in user controls ). The second is cache. It provides a way to cache partial page information, such as adding a declaration in the web user control header:

<% OutputCache Duration = "300" VaryByParam = "none" %>

 

The third point is that the user control also provides a good AJAX call method. Someone used the ajaxHelper method on the Internet to load the ascx file and obtain the corresponding response information, load and refresh the specified area on the page (usually the div element )."

"I agree with the first two points, but what do you mean by the third point? How can I not quite understand it ?" Old Huang is puzzled.

Yu Chen explained: "In fact, the principle is very simple, that is, you are calling ajax. put a form tag on the aspx page, whose runat is "server", and then in ajax. aspx. in the Page_Load event of the cs file, the controls attribute of the form is used, and the add method of this attribute is called to load the specified user control (passed as a parameter). In this case, the ajax. the aspx page displays the output content of the specified user control. Of course, in ajax. the aspx page contains a tag to record the 'region' of the user control display information on the current page. In this way, prototype is used. when using ajax functions in such frameworks as js, you can directly access ajax. the aspx page passes the 'parameter' of the user control to be called, and captures the page information returned after the control is loaded as the return result of the ajax call. It is used to refresh the specified page element (usually div) innerHtml attribute in. After talking about this, I will give you a link. You can see its final implementation Code (Code link)

After hearing Yu Chen say this, Lao Huang understood what was going on and said, "but I feel this ajax. aspx is like a proxy, but it also provides a good idea. Because I used to use ajax to request an executable page or link, such as aspx, ashx, or asmx ."

Yu Chen said with a smile: "This also shows that the development of web user controls is very similar to that of web pages. In addition, when learning and developing user controls, we often make some minor mistakes ."

According to Yu Chen, Old Huang asked, "What are the problems ?!"

"The first is to add some special business logic to the user control code, reducing the versatility of the control. The second is that some people are used to placing only one LABLE on the web user control, and then use the CS file to fill in its. text attribute. This is not a good habit. Controls such as Repeater and Datagrid should be used for complex data presentation, and even several controls should be composite to meet the requirements. This approach of hard coding data and display control code should be adopted, further code separation and reading and understanding will cause a lot of trouble. In addition, I found that some developers are used to putting all the CS code of the web user control into the Page_load event, but because the CS file of the user control also contains methods such as OnInit and InitializeComponent (similar to page ), code should be evenly distributed among the corresponding Event code based on the functions to be implemented, such as initializing component code, binding data to the control code, and displaying code on the page. These three problems are only part of them and should be representative ." Yu Chen was thinking about it.

Lao Huang nodded. However, I still have a problem. Previously, I constructed a base class for all web user controls in the project and provided some public attributes in this base class, such as the control version number, control name and initialization of some common attributes, so that the initialized attributes can be directly used in its derived subclass (control. Of course, I also want to write some jump logic to this control base class. For example, in management background operations, if a problem occurs in user authentication, the jump can be completed, instead, you cannot jump back when you execute page_load on the homepage. I don't know if there is any problem with this operation."

Yu Chen said with a smile: "I have no objection to the practice of extracting a control base class, but there are some differences in the idea of 'whether to provide jump operations. Of course, writing in this way can also complete the functions you want to implement. However, I think that operations such as user identity and permission verification should be placed on the homepage or business logic layer of the control, because the control only performs Data Binding and data display, if too many special business logic is added, the versatility of the control will be reduced. In addition, if you want the control base class to perform this type of permission class operation, you must also provide the corresponding 'tog' attribute, because not all application scenarios require user permission verification, this also increases the 'difficulty coefficient 'of the control, and even causes ambiguity. Of course, if you only want to use this type of control for the ajax call I mentioned earlier, you can still say it, because it is similar to directly calling the aspx page, so when the client requests the user control, it is a good idea to return the corresponding results or verify the error information by verifying the identity information it provides."

Lao Huang said with a smile: "Where do you want to place code like page Jump ?"

Yu Chen thought about it and wrote something on the blackboard next to it. The content is as follows:

Home Page Properties> Home Page constructor> User Control Properties> User Control constructor> OnInit event of user control> InitializeComponent method of user control> OnInit on Home Page event --> InitializeComponent method on the home page --> Page_Load () on the home page () --> Page_Load () of the user control page ()

 

Then he turned around and said to Lao Huang, "this is the execution process for pages with user controls. If I want to write the permission verification logic, I will put it in step 2, that is, in the constructor on the home page, you can stop executing the subsequent control initialization and page_load methods when the 'verification operation' fails, it is believed that the speed will be much faster, but if Response is used in the constructor. redirect (...) This kind of writing may cause problems. I suggest using System. Web. HttpContext. Current. Response. Redirect (), which is almost the same ."

Lao Huang looked at the execution process and thought about the code he developed. He said, "I will test the process later. If, as you said, there will be more places to change, huh, huh."

Yu Chen went on to say: "In fact, developing controls requires a certain understanding of the lifecycle of pages and controls. In particular, the development of custom controls. Because the user control provides. ascx and the corresponding. ascx. cs file, which makes it easy to separate the data binding and display logic from the Code. At the same time, it has many similarities with webform page development, and even makes no difference when you get started, so it is not very difficult. The custom control is different. It does not have front-end interface files (such as ascx), and all the code is corresponding. for example, to develop custom controls used in web pages, you must inherit WebControl or Control and implement the Render (HtmlTextWriter pOutPut) method."

Yu Chen took the water cup and took a sip of water and continued: "In addition, I prefer to add more business logic to user controls, the business logic in custom controls is not that important, because custom controls should emphasize their versatility and ease of use, for example, drag a custom control such as lable into the user control. In addition, problems such as webpage and control lifecycle, event mechanism, return event, view status, and related process functions should be considered in custom control development. Therefore, I feel that custom control development should focus more on the universality of the control (for example, it can be used in both winform and webform) and the understanding of the underlying implementation mechanism of the control. Some people say that developing custom controls is more complex than user controls, because of this ."

Lao Huang said: "Indeed, I also think that web-based custom controls also need to understand page lifecycle-related content. Currently, I am still developing and designing user controls, I am not involved in custom controls."

Yu Chen smiled and said, "This is just a matter of time. In addition, I have experienced four stages in developing controls over the years:

1. in the Getting Started stage, all code is put in a cs or a class, and the class-level definition is broken down based on its control behavior and structure complexity, for example, when you want to develop a tab control, you should split the corresponding attribute pages into a cs file, in this way, you can regard the entire tab control as a 'collection' of an attribute page for 'composite process' (use CreateChildControls to create a subcontrol ).

2. Looking at the code of the open source or cracked third-party commercial control library (some immoral, but a learning path), we found that what we write is rubbish, and we can see the gap with others. I feel that my level is like a primary school student, and my self-confidence has suffered an unprecedented blow, and I dare not develop any more.

3. clean up the self-confidence of the broken, study the corresponding excellent code, and study the underlying implementation (for example, use reflector. net controls 'source', and look at some more. the controls and their implementation code (such as the MFC and DELPHI control libraries) before the emergence of net, from the underlying understanding to a higher level.

4. provides rich 'Design-time support 'for controls, such as using PropertyGrid (Attribute Table) and inheriting and implementing the ControlDesigner class (located in System. web. UI. design namespace) and override the GetDesignTimeHtml () method to allow users to initialize the control 'manually 'in the designer of, in this case, we can think about how to manually add Tree nodes when using controls such as TreeView.

Of course, you must develop web controls and have a certain understanding of css and js. Sometimes style or JS implementation is more convenient, flexible, And customizable than cs code. However, in this regard, commercial controls should consider js script encryption and obfuscation ."

Lao Huang nodded and said, "In fact, I can write a few books about how to develop controls. When I look at a list control from a foreigner, I can sell hundreds of dollars, I feel like there is a lot of money."

Yu Chen smiled and said, "Who said no! In addition, control developers outside China all have products with rich functions and gorgeous interfaces, such as ComponentArt, telerik, devexpress, componentone, infragistics, netikatech, etc, in addition, its R & D pace is often closely following IT giants such as Microsoft, such as the development of components related to silverlight technology. I think IT is quite "moist '. However, the survival status of control developers in China is not very optimistic. This product must be supported by technology, and its 'customer' is often used by developers, it is more 'behind-the-start' than software developers in the industry '. In addition, you have too many dependencies and misunderstandings on the software 'free '. Therefore, the commercialization of controls through development in China is more difficult than the commercialization of software, or even 'unsolvable '. For example, you may have the following opinions on a commercial and easy-to-use control library:

1. First, think of the cracked version (it is better to have the source code ).

2. Consider other ways to use the control for free.

3. If there is no other way, you can develop a similar control (the development cycle is long or the functions are too complex)

4. If this cannot be done, check whether there are any open-source projects as alternatives. Sometimes, due to the reluctance to pay, the software interface design is eventually changed, causing problems for users, poor user experience, and even BT ."

Lao Huang said with a smile: "I basically agree with what you have said, but I still hope that commercial control companies in China can develop well. This is definitely a direction, previously, I saw a Chinese company on csdn proposing to develop a service-based component (actually a control). In other words, I only need to drag several components (or controls) to a form or page, setting a property is simple and the development is complete. How convenient is it!"

Yu Chen said with a smile: "If you really want to have that day, you and I will all be unemployed, because by then the boss or business experts can complete software development, what are you doing."

At this time, yuchen looked at the time, and after a few greetings, he went back to his seat to work.

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.