I needed a validation tool that could be reused in multiple projects, so I decided to make it a Web User Control. Below are the steps I took to set it up:
- Created a new web project and added my Web User Control.
- In IIS, I enabled the project as an application, and set this new project folder as a Virtual Directory in all the projects that will be using it, and enabled each Virtual Directory as an application.
For each project that uses the control, I added a reference to the dll, and added the control to that project's
web.config
:<add tagPrefix="ut" src="~/UserControls/ValidateAcct.ascx" tagName="ValidateAcct" />
NOTE:
'UserControls'
is a Virtual Directory, and does not actually exist in each of the host projects.Then I add开发者_如何学编程ed the control to the host page:
<ut:ValidateAcct ID="valAcct" runat="server" />
At runtime the content from the Web User Control displays correctly on the host page, but from the host page's code-behind I am unable to call any of the methods I created for the Web User Control. If I instantiate the control entirely from code-behind, the methods are there. I assume this is because I am working solely from the dll, and doesn't involve any Virtual Directory.
Another issue I am encountering is with events. I created the following event on the Web User Control, and I'd like to handle it on my host page:
protected virtual void AccountValidated(object sender, EventArgs e) { }
Once again, from the host page's code-behind I cannot access this event. I thought maybe I could just instantiate the control in the code-behind to access the event, like I did with the methods, but even that didn't work.
The goal here is that I'd like this user control to be used by multiple projects within one website, but only have to maintain one copy of the control/code. This is why I went the Virtual Directory route, but I have a feeling this is the cause of all my problems. Any assistance anyone can give would be greatly appreciated!
You need to create event event handlers in the user control so that you can bubble the events up the page.
public event EventHandler AccountValidated;
As for your second problem, can you check the designer.cs file and make sure that the user control type is ValidateAcct. If it's not ValidateAcct, and it's just UserControl, then that's why you can't see any of the methods you created.
You don't need to set up a web directory to share code across projects.
In fact, this is a very wrong approach.
It is indeed tricky to share a User Control but you'll succeed if you follow these instructions:
- Write your User Control as you normally would, typically using the Visual Studio designer.
- Test it using a simple page before trying to deploy it.
- Deploy the app to precompile it.
- Grab the user control's assembly produced by the deployment step, and you're essentially done: you have your Custom Control.
- Finally, use your Custom Control in other apps the same way as you always use Custom Control's.
There is also another post on this topic which contains some MSBuild automated goodness.
I would try it first because it seems to be a simpler way but I'm not 100% sure it works.
精彩评论