views:

56

answers:

4

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.

+1  A: 

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.

Daniel A. White
actually yes 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 ?
Stacker
It gets compiled into your `.dll` file in the `bin` directory.
Daniel A. White
@ZeusSE See my answer below as to where, `When the page is compiled, ASP.NET generates a partial class based on the .aspx file;`
Kelsey
+1  A: 

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.

Femaref
A: 

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.

Jon Hanna
+1  A: 

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?

Kelsey
i will just make a type property, it seems it wont happen as a generic although it should. thanks Kelsey.
Stacker
Could you achieve what you want by declaring a generic type *nested inside* the `UserControl` class?
Timwi