3

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.

Stacker
  • 8,157
  • 18
  • 73
  • 135
  • Please, explain more why do you have to use generics? It has to be partial for designer. Designer puts runat="server" components declarations, that are seen as private members in Control event handlers – Andrew Florko Aug 19 '10 at 00:42

4 Answers4

1

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.

Femaref
  • 60,705
  • 7
  • 138
  • 176
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 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 Aug 19 '10 at 00:50
  • It gets compiled into your `.dll` file in the `bin` directory. – Daniel A. White Aug 19 '10 at 00:56
  • @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 Aug 19 '10 at 01:07
1

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
  • 60,705
  • 7
  • 138
  • 176
1

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
  • 47,246
  • 16
  • 124
  • 162
  • i will just make a type property, it seems it wont happen as a generic although it should. thanks Kelsey. – Stacker Aug 19 '10 at 01:14
  • Could you achieve what you want by declaring a generic type *nested inside* the `UserControl` class? – Timwi Aug 19 '10 at 02:15
0

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
  • 110,372
  • 10
  • 146
  • 251