Search code examples
c#asp.netuser-controlswebformspartial-classes

Partial User Control


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 because im making a Grid User Control so i want to pass the type the datasource is going to have.


Solution

  • 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?