+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Call Method of user control

Call Method of user control

5 posts, 0 answered
  1. Shawn Davis
    Shawn Davis avatar
    81 posts
    Registered:
    13 Oct 2012
    07 Aug 2010
    Link to this post
    I've looked but haven't found anything exactly on point for  this.  I have a Sietfinity page that has two user controls on it.  I would like to call a method on one from the other.  I know this is doable I just can't figure it out.  can you help me?

    Thanks in advance!
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    08 Aug 2010
    Link to this post
    Hi Shawn Davis,

    You can find the controls on your page and then create an object of your user control type, then call a method from it.

    IList<Telerik.Cms.ICmsWebControl> pageControls = currentPage.Staged.Controls;
    foreach (Telerik.Cms.ICmsWebControl webCtrl in pageControls)
    {
        if (webCtrl.ControlType == typeof(MyUserControl))
        {
            MyUserControl gcCtrl = webCtrl.LoadControl() as MyUserControlt;
            gcCtrl.MyMethod();
        }
    }


    It is better to use custom control instead of a user control. When you use a user control you have to add reference to it inside the ascx or aspx you use to add your custom logic, otherwise you cannot webCtrl toMyUserControl

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Shawn Davis
    Shawn Davis avatar
    81 posts
    Registered:
    13 Oct 2012
    10 Aug 2010
    Link to this post
    I placed this at the top of the .ascx for this control:

    <%

    @ Register Src="~/UserControls/PTO/Cart.ascx" TagName="Cart" TagPrefix="pto" %>

    But when I use : if(webCtrl.ControlType == typeof(Cart)) it tells me Cart could not be found.
    What am I missing?

     

  4. Shawn Davis
    Shawn Davis avatar
    81 posts
    Registered:
    13 Oct 2012
    10 Aug 2010
    Link to this post
    It also says the name currentPage doesn't exist in the current context.  I assume I'm missing a namespace reference.  If you weren't here to tell me, how would I figure out what namespace that belongs to?

    Thanks.
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    10 Aug 2010
    Link to this post
    Hello Shawn Davis,

    • currentPage does not belong to any namespace. This is the current ICmsPage object on which you have dropped your control.

    • You have to add reference to your control as shown below

    <%@ Reference VirtualPath="PATH TO YOUR CONTROL"%>


    Sincerely yours,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
5 posts, 0 answered