Title: Programatically load a UserControl from another project
Tags: asp.net
Question: I like to build a components collection project so that I can use those components later on other projects. For example I created a UserControl called MyDataGrid.ascx

So suppose the projects layout are something like this:

```
  --> IPGostarProject - Under namespace of IPGostar - contains components and
  lot's of user controls
  
  --> SampleProject - here I want to use MyDataGrid.ascx UserControl from
  IPGostar namespace.
```

But as you know LoadControl function only takes virtual directory as it's parameter. So you can't load anything outside the SampleProject directory! 

So how can I do that?



Here is another answer: As long as you're dealing with partial classes there's no great way to do this outside of file system trickery. A better way is to implement shared UserControls as WebControls. If you have extensive markup, it's probably not well suited to be a shared control, anyway... e.g. will you also share the style sheet?

Separate out the majority of the markup which is not part of the core functionality of your control, and implement just the core as a WebControl, generating any markup and controls in code. The actual project that implements it should be responsible for layout, formatting, etc. Use events to provide hooks to the client (e.g. for a data grid, events for CreateRow, CreateCell, and so on)


Here is another answer: One way would be for the post-build event (or whatever build script you are using) of SampleProject project to copy the actual user controls into a subfolder of SampleProject so they are tateselena@example.com.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2010:00:12/10/10', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<asp.net>', 'Title': 'Programatically load a UserControl from another project', 'ViewCount': 422, 'id': 4412015, 'user_AboutMe': '', 'user_AccountId': 43506.0, 'user_DisplayName': 'EBAG', 'user_Reputation': 20625.0, 'user_Views': 932.0, 'user_id': 127193.0}}