Mobile Project Development notes (a user control triggers thoughts on ASP.net 2.0 compilation)

Source: Internet
Author: User

I. cause:
A developer from the development team suddenly resigned and handed over the module tasks he was in charge of to me. One module is a user control that I can't afford to use on my pages. An error occurs during running. The error message is:The base class implements des the field *****, but its type (****) is not compatible with the type of control (Asp. webusercontrol_ascx ).
 
Ii. Process:
When this problem occurs, I personally feel that the user control is not successfully registered on the page. Found the page registration code:

<% @ Register src = "projectinfo. ascx" tagname = "projectinfo" tagprefix = "uc3" %>
<Uc3: projectinfo id = "projectinfo1" runat = "server"/>

I found that this code is correct through repeated searches. I also found that the previous user control page was registered in this way. So what is the problem? It was easy to find the problem. The statement of the user control was written in this way.

<% @ Control Language = "C #" autoeventwireup = "true" codefile = "projectinfo. ascx. cs" inherits = "projectinfo" %>

The problem lies in codefile. Because the project is a Web application, the declared user control should be codebehind. How can it be converted into codefile? Finally, the problem is found. After I change codefile to codebehind, the page will be displayed normally. Who modified it to codebehind. By viewing the vsts modification history, no one modified the file. It was already codefile when the file was added. By default, all user controls added by web application are codebehind. Unless it is not a web application, I created a web site and added a user control:

<% @ Control Language = "C #" autoeventwireup = "true" codefile = "projectinfo. ascx. cs" inherits = "projectinfo" %>

The Source Page of the problem is found. It turns out that this user control was transplanted through the web site. Thank God for solving this problem.
Iii. Thinking
The problem is found. The problem Source Page is found. So why can't codefile be used here. This is the essence of the problem. Why codebehind instead of codefile is used to declare the control or page declared by web application?
Iv. Results

By constantly searching for the cause, I finally found the web application with codebehind and the web site with codefile?
In the past, the compilation mechanism of Web application and web site was different.
Web site: When we use vs.net to create a page, Vs will automatically create a codebehind file, but when we compile the site, vs.net will automatically according to the content of the page file, generate a temporary "partial" class, compile the partial class and the partial class in codebehind, and generate a DLL file in the bin. (Implemented by codefile)
Web Application: When we add a new control to the page, vs.net automatically adds a partial class of the protected object to the codebehind code ), when this object is used and operated in the code, vs.net will maintain the synchronization between the control of the page file and the declaration of the partial class code file at all times. When we compile the site, vs.net will compile the codebehind file together to generate a DLL file located in the bin directory. (Codebehind)

After the comparison above, we now know that the above user control statement should use codebehind instead of codefile. The root cause is that the Web application page uses the codebehind method. If the user control is compiled by codefile, there is a conflict, the page takes precedence over the user control method. You can directly compile all the code files into DLL files, instead of generating a partial class for the page file and then compiling it together with the code file. Therefore, the user control of the page will not be generated by vs.net Based on the page content and the partial class in codebehind will be integrated and compiled. The lack of a statement on the user control will result in:The base class implements des the field *****, but its type (****) is not compatible with the type of control (Asp. webusercontrol_ascx ).

Other references: http://blog.csdn.net/ups216/archive/2006/09/06/1186835.aspx

 

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.