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

Forums / General Discussions / use Findcontrol to find user control

use Findcontrol to find user control

10 posts, 0 answered
  1. Jay Mehta
    Jay Mehta avatar
    19 posts
    Registered:
    02 Sep 2009
    01 Nov 2010
    Link to this post
    Hi ,

    I have registered a control from web.config file, using it on a page.
    I have another user controls which are registered in it. I need to find the user control and refer to this registered user control to access literal values.

    I am getting object reference error while doing that.
    below is my code..below is my code..
    In the code "quickScore" is the user control which I drop from my toolbox.
    "QuickWizard" is a wizard control in "quickscore" control. I have "screen_103_ctl" as a registered control in "quickscore". I need to access Literal control from the "screen_103_ctl".  PLease suggest something....it is urgent.

    Thank you.
    Dim uc As UserControl = Page.FindControl("quickScore")
           Dim wiz As Wizard = uc.FindControl("QuickScoringWizard")
     
           Dim uc1 As UserControl = wiz.FindControl("screen_103_ctl")
           Dim lit1 As Literal = uc1.FindControl("Literal1_103")
           Dim lit2 As Literal = uc1.FindControl("Literal2_103")
           Dim lit3 As Literal = uc1.FindControl("Literal3_103")
     
           Literal1_104.Text = lit1.Text
           Literal2_104.Text = lit2.Text
           Literal3_104.Text = lit3.Text
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    01 Nov 2010
    Link to this post
    Hi Jay,

    You can use recursion or create a control container for your control (control that is used as containers for other controls defined through control templates (ITemplate))

    If you use Sitefinity 4.0 you can use ControlTraverser - transforms a control hierarchy to a collection by traversing the hierarchy with a specified algorithm

    ControlTraverser traverser = new ControlTraverser(this, TraverseMethod.DepthFirst);


    Greetings,
    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. Jay Mehta
    Jay Mehta avatar
    19 posts
    Registered:
    02 Sep 2009
    01 Nov 2010
    Link to this post
    Thanks Ivan......can you please provide some relevant code to find the control I drop on the sitefinity page and then find a control inside that....and then find a control inside that control :)...or is there any sample project which I can refer too.
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    01 Nov 2010
    Link to this post
    Hi Jay ,

    You can get the page node and then loop through its controls collection.

    var pm = PageManager.GetManager();
    var node = pm.GetPageNodes().Where(n => n.Title == "ControlsNode").SingleOrDefault();
    var g = node.Page.Controls.Where(gt => .....);

    You could implement GenericContainer  - represents base implementation for template container.

    internal protected virtual GenericContainer Container
    {
        get
        {
            if (this.container == null)
            {
                this.container = this.CreateContainer(this.LayoutTemplate);
            }
            return this.container;
        }
    }
     
    /// <summary>
    /// Creates the container.
    /// </summary>
    /// <param name="template">The template.</param>
    /// <returns></returns>
    protected internal virtual GenericContainer CreateContainer(ITemplate template)
    {
     
        GenericContainer container = new GenericContainer();
        template.InstantiateIn(container);
        return container;
    }
     
    #endregion

    Then you will be able to use it as shown below

    protected virtual Control Test
     {
         get { return this.Container.GetControl<Control>("Test", false); }
     }


    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
  5. Jay Mehta
    Jay Mehta avatar
    19 posts
    Registered:
    02 Sep 2009
    02 Nov 2010
    Link to this post
    Thanks Ivan,

    Do I implement this in my regular .net user control and then upload the modified control into sitefinity?

    or do you have some working project where I can refer to?

    Thank you very much
    Jay Mehta.
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    02 Nov 2010
    Link to this post
    Hello Jay,

    You should use a custom control - class library. If you inherit from SimpleView then you will be able to use the built-in container that recursively looks inside all controls from your template.

    Greetings,
    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
  7. Jay Mehta
    Jay Mehta avatar
    19 posts
    Registered:
    02 Sep 2009
    02 Nov 2010
    Link to this post
    Here is the code snippet I have included in my user control.....let me know if this should work when I upload this control on to sitefinity. or How do I modify this code to make it work with your suggestion.

    This code is able to find the contentplaceholder from the master template, but then is not able to find the control which I uploaded to the page..."ucQuickWizard" (This is the name I have mentioned in the web.config in the tools section)...."screen_103_ctl" is a user control sitting in one of my folders on the server.


    Dim mpContentPlaceHolder As ContentPlaceHolder
    Dim uc As UserControl
    Dim lit1 As New Literal
    Dim lit2 As New Literal
    Dim lit3 As New Literal
    mpContentPlaceHolder = CType(Page.Master.FindControl("mainbody"), ContentPlaceHolder)
    If Not mpContentPlaceHolder Is Nothing Then
        uc = CType(mpContentPlaceHolder.FindControl("ucQuickWizard"), UserControl)
        If Not uc Is Nothing Then
            Dim wiz As Wizard = uc.FindControl("QuickScoringWizard")
            Dim uc1 As UserControl = wiz.FindControl("screen_103_ctl")
            lit1 = uc1.FindControl("Literal1_103")
            lit2 = uc1.FindControl("Literal2_103")
            lit3 = uc1.FindControl("Literal3_103")
            Literal1_104.Text = lit1.Text
            Literal2_104.Text = lit2.Text
            Literal3_104.Text = lit3.Text
        End If
    End If
  8. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    03 Nov 2010
    Link to this post
    Hi Jay Mehta,

    As Ivan has noted you have to look through the whole control collection in the content place holder. It might turn out that your control is situated in a child control of the control place holder.

    All the best,
    Radoslav Georgiev
    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
  9. Ankit
    Ankit avatar
    1 posts
    Registered:
    03 Nov 2010
    03 Nov 2010
    Link to this post
    Thank you Radoslav.
    I am working on that issue now.
    You are right, our control is under child control of content place  holder.

    Main Issue: if we try to find that control without sitefinity using local website,it's working fine.But when we try to find control with same working code it gives "Object reference not set to an instance of an object". 

    Basically we try to find "screen_103_ctl" control which is under 

    Page-->"mainbody"(ContentPlaceHolder) --> ucQuickWizard(user Control) --> QuickScoringWizard(Wizard Control) --> screen_103_ctl(Usercontrol)

    We can find ContentPlaceHolder(mainbody) in sitefinity but when we reached at ucQuickWizard Contol(Line 8), we got error in sitefinity.

    Code snippet: 

    1 Dim mpContentPlaceHolder As ContentPlaceHolder
    2 Dim uc As UserControl
    3 Dim lit1 As New Literal
    4 Dim lit2 As New Literal
    5 Dim lit3 As New Literal
    6  mpContentPlaceHolder = CType(Page.Master.FindControl("mainbody"), ContentPlaceHolder)
    7  If Not mpContentPlaceHolder Is Nothing Then
    8    uc = CType(mpContentPlaceHolder.FindControl("ucQuickWizard"), UserControl)
    9        Dim wiz As Wizard = uc.FindControl("QuickScoringWizard")
    10        Dim uc1 As UserControl = wiz.FindControl("screen_103_ctl")
    11        lit1 = uc1.FindControl("Literal1_103")
    12        lit2 = uc1.FindControl("Literal2_103")
    13        lit3 = uc1.FindControl("Literal3_103")
    14        Literal1_104.Text = lit1.Text
            Literal2_104.Text = lit2.Text
            Literal3_104.Text = lit3.Text
        End If
    End If
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    03 Nov 2010
    Link to this post
    Hi Ankit,

    Could you send the HTML of the control, so we could see whether the issue comes from Sitefinity. It is strange that the controls collection for you is empty and you get null reference exception.

    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
10 posts, 0 answered