[RESOLVED] Register user control in web config, reference in code
I have registered a user control in my web config as below, then on my aspx page i place an instance of the control
Code:
<asp:Content ID="Content2" ContentPlaceHolderID="maincontent" Runat="Server">
<AG:MenuControl ID="MenuControl1" runat="server" />
</asp:Content>
Code:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<pages>
<controls>
<!-- Global Sourcing Platform user controls -->
<add tagPrefix="AG" tagName="MenuControl" src="~/usercontrols/Menu.ascx" />
</controls>
</pages>
This works fine and the code builds and displays the control as desired.
The problem comes when I want to reference the control in code.
i.e.
Code:
ASP.usercontrols_Menu testvar;
It just does not appear in intellisense or allow build when trying to reference the usercontrols class. I get the error
Quote:
Error 1 c:\Outlines\Outlines\usercontrols\Menu.ascx.cs(36): error CS0234: The type or namespace name 'outlines_usercontrols_menu_ascx' does not exist in the namespace 'ASP' (are you missing an assembly reference?)
Please could someone tell me what I am missing here.
Re: Register user control in web config, reference in code
Code:
<%@ Reference Control="~/Outlines/usercontrols/Menu.ascx" %>
That fixed the original error, my problem now is that I have a circular reference.
I have control a reference control B which also needs to reference control a. Is there anyway round this?
The reason for the references is because I wanted to set properties of the controls on postback and use the previouspage method to retrieve the values of the controls properties when moving between pages. This causes a problem with both controls needing to reference each other.
Re: Register user control in web config, reference in code
Hello FishGuy,
I am having a problem visualising the problem that you are having. Can you perhaps upload a small sample project that shows the problem that you are having?
Thanks
Gary
Re: Register user control in web config, reference in code
Ok I have two seperate pages each has a seperate user control on.
I want to be able to set a value on the usercontrol on the first page and access it in the second user control when I navigate to the next page.
I have found this is pretty straight forward if i set a public property and then use put a reference to the first user control in the second I can use the previous page method and find conrol.
Code:
if (Page.PreviousPage != null && Page.PreviousPage.IsCrossPagePostBack)
{
ContentPlaceHolder BodyContent = (ContentPlaceHolder)Page.PreviousPage.Master.FindControl("maincontent");
ASP.courseoutlines_usercontrols_searchoutlines_ascx test = (ASP.outlines_usercontrols_searchoutlines_ascx)BodyContent.FindControl("SearchOutlines1");
if (test != null)
{
Code = test.Code;
However if the user navigates back to the original page I would want to set a value on this control and have it accessible from the original control, however to do this as above it would also require a reference to the second control meaning both controls have to reference each other. This is not allowed.
I have kind of gotten round this by using session variables to pass the values between the controls instead but wondered if there was a better method?
Re: Register user control in web config, reference in code
Ah, I see what you are referring to now.
In all honesty, I don't think that there is a "better" way than what you are already doing with Sessions.
Gary