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:
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 added 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.