开发者

Partial User Control

开发者 https://www.devze.com 2023-01-11 23:40 出处:网络
In Asp.net Web forms why must a user control be partial? I tried to remove the partial keyword and it says:

In Asp.net Web forms why must a user control be partial? I tried to remove the partial keyword and it says:

Missing partial modifier on declaration of type 'test'; another partial declaration of this type exists

Where is the other declaration?

I am trying to pass a generic type with the user control how can I do that? I can't unless I change the other declaration too. I couldn't find it so I removed the partial keyword.

Note: you do have 3 files if your making WebApplication but if your making website you only get 2 files !

UserControl.ascx
UserControl.ascx.cs 

so in website where is the other declaration ?


the reason i want generic is beca开发者_JS百科use im making a Grid User Control so i want to pass the type the datasource is going to have.


ASP.NET uses partial classes for the codehind because it has to generate all the server side controls you have declared in your ASPX file to a class and merge all the other data files that come along with your codebind. It allow the classes ASP.NET uses to be distributed across multiple files.

From MSDN:

The code file contains a partial class—that is, a class declaration with the keyword partial (Partial in Visual Basic) indicating that it contains only some of the total code that makes up the full class for the page. In the partial class, you add the code that your application requires for the page.

This is the key part:

When the page is compiled, ASP.NET generates a partial class based on the .aspx file; this class is a partial class of the code-behind class file. The generated partial class file contains declarations for the page's controls. This partial class enables your code-behind file to be used as part of a complete class without requiring you to declare the controls explicitly.

What are you trying to do by adding a generic type to a user control? Can you accomplish this by adding a Type property to the UserControl and then using that?


They have to be partial because the designer autogenerates the stuff you do in the Design window. Usually that code is in the foo.designer.cs (or whatever the extension for an asp.net form is). If you want to change the class, you have to generate the UI manually without the designer or use the designer and copy the code over to your proper class.


There are actually three files for each user control.

UserControl.ascx
UserControl.cs
UserControl.designer.cs

The designer file has a partial implementation because of that your code behind also has to be partial.


They don't have to be partial, indeed I've never used a partial class for one. Some of the code-generation tools will use partial so that they can more readily alter bits based on GUI-work you do without you and it getting in each others way, but that's for the tools rather than for the code.

0

精彩评论

暂无评论...
验证码 换一张
取 消