Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • gluc86 23 posts 123 karma points
    Mar 02, 2014 @ 17:38
    gluc86
    0

    Modify Html property in UserControl

    Hi guys, I would like to change the property of an HTML element that is in the page from my UserControl. 

    The problem is that the HTML element is not in my UserControl so if I try to use a "FindControl" I have reference a non-existent : "Object reference not set to an instance of an object."

    In my code i tried this :

    Control monDiv = Page.FindControl("panelNewsProspect");
    monDiv.Visible = false;

    Or this :

    Control monDiv = Page.FindControl("panelNewsProspect");
    monDiv.Attributes.Add("style","visibility:hidden;");

    In my page i tried this after my macro :

    <div ID="panelNewsProspect" runat="server">

    Anyone have a solution ?

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Mar 03, 2014 @ 09:39
    Ismail Mayat
    0

    gluc,

    Not really an umbraco question however you can do a recursive lookup for that control some code:

            /// <summary>
        /// get find control method using generics
        /// </summary>
        /// <typeparam name="T">type of item you want</typeparam>
        /// <param name="ancestor">parent to start looking from</param>
        /// <returns>collection of T items</returns>
        public static List<T> ControlsByTypeUsingAncestor<T>(Control ancestor) where T : Control
        {
            var controls = new List<T>();
    
            //Append to search results if we match the type 
            if (typeof(T).IsAssignableFrom(ancestor.GetType()))
            {
                controls.Add((T)ancestor);
            }
    
            //Recurse into child controls 
            foreach (Control ctrl in ancestor.Controls)
            {
                controls.AddRange(ControlsByTypeUsingAncestor<T>(ctrl));
            }
    
            return controls;
        }
    

    So to get at that div you would do something like

    var panel = ControlsByTypeUsingAncestor<HtmlGenericControl>(Page).Where(c=>c.Id=="panelNewsProspect").FirstOrDefault();
    

    So what this code will do is for the Page (it can be any control does not need to be page but in your instance you will need to pass in page) you pass in it will recursively get all the HtmlGenericContrls then we are doing a linq lookup by id to get the actual control we want. Code example may need a bit of tweaking but the method is sound i have used it on loads of projects.

    Regards

    Ismail

  • gluc86 23 posts 123 karma points
    Mar 03, 2014 @ 12:09
    gluc86
    0

    Hi Ismail,

    This is an umbraco question because normaly the "Page.FindControl()" should work.

    I have already try a find control recursive as your code, but it doesn't work.

    If I try to see the different control in the "ControlsByTypeUsingAncestor", I just see the control "page" but nothing else ...

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Mar 03, 2014 @ 12:52
    Ismail Mayat
    0

    gluc,

    Page.FindControl is not recursive so if your control is in a panel or something else then it wont work. I had similar issue what a cart application where i was adding stuff to a basket and after add i needed to update my basket summary which was on another control so i used the controlsbytypeusingancestor to get the summary panel in the code behind of my basket control. Can you paste your code for controlsbytypeusingancestor.

    Regards

    Ismail

  • gluc86 23 posts 123 karma points
    Mar 03, 2014 @ 14:43
    gluc86
    0

    I try exactly your code.

    I tried to call the "ControlsByTypeUsingAncestor" with an HtmlGenericControl or an Control. My control isn't find.

    I tried to add an another div in my UserControl and it works for this "div".

    The difference between them,this is the first "div" is in the content of page and it's create in a textEditor.

    The strange thing is when i see the collection of "ControlsByTypeUsingAncestor<HtmlGenericControl>(Page)", i find only 2 elements. The "head" and my second div (in my UserControl). Yet, i have many html control in the page.

     

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Mar 03, 2014 @ 16:32
    Ismail Mayat
    0

    the div in text editor not sure what you mean but is that div setup so that its a server control i.e runat="server" ?? Also where are you making the call ControlsByTypeUsingAncestor is this in another usercontrol?

    Regards

    Ismail

  • gluc86 23 posts 123 karma points
    Mar 03, 2014 @ 17:03
    gluc86
    0

    Here is a short explanation of what I'm trying to do.
    I have a page with editable content by the site administrator in edit mode (with TextEditor).
    I have a macro (with a UserControl) that after treatment must make a "div" visible.
    So I call my macro in the TextEditor then below I place my div to hide.
    So I have my "div" which is outside the UserControl. I have added the runat = "server" to the div to hide but there is no list in the list of controls found ControlsByTypeUsingAncestor ...

    You understand what I'm trying to do?

    Regards

    Gluc

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Mar 03, 2014 @ 18:11
    Ismail Mayat
    0

    So the usercontrol macro is inserted into rich text edit field which you are calling texteditor? And in this macro you are trying to do the get div using ControlsByTypeUsingAncestor? If that is the case then maybe due to page lifecycle it may not work. For testing purposes try putting the macro in the template and see if that works.

    Regards

    Ismail

  • gluc86 23 posts 123 karma points
    Mar 03, 2014 @ 18:51
    gluc86
    0

    That's right. I'll try to put the macro directly into the template.

    Regards

    Gluc

  • gluc86 23 posts 123 karma points
    Mar 04, 2014 @ 15:49
    gluc86
    100

    It works ^^.

    The problem don't come to the location of macro but of the location of my div.

    I tried to put my div into the template (around my texteditor) and it works. Umbraco must rewrite my div if it's in the texteditor.

     

Please Sign in or register to post replies

Write your reply to:

Draft