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;");
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.
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.
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.
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?
Here is a shortexplanation ofwhat I'm tryingto do. I havea page witheditable contentbythe site administratorin edit mode(withTextEditor). I have amacro (with aUserControl)thatafter treatmentmustmakea"div"visible. So Icall mymacroin theTextEditorthen belowI placemydivto hide. So I havemy"div"whichis outside theUserControl.I haveaddedtherunat= "server"to thedivto hidebutthere is nolistin the list ofcontrolsfoundControlsByTypeUsingAncestor...
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.
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 :
Or this :
In my page i tried this after my macro :
Anyone have a solution ?
gluc,
Not really an umbraco question however you can do a recursive lookup for that control some code:
So to get at that div you would do something like
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
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 ...
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
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.
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
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
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
That's right. I'll try to put the macro directly into the template.
Regards
Gluc
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.
is working on a reply...