User control macro on master page - how to access its properties from another user control?
I have a "StandardPage" master page, and I've placed a macro on it which points to a user control "Message.ascx". Let's say this control contains an asp:Label control.
On a different master page, which uses StandardPage as *its* master page, I have another macro which points to another separate user control. I need this second control to be able to set the Text property of the asp:Label on the first user control.
Yes this is possible. You can fetch another usercontrol and do stuff with that. Here is a sample using macro's:
Recursive method to find the control:
/// Find a control and return it as T.
/// This controls goes recursive through all controls until the control is found.
public static T FindControlRecursive(this Control control, string controlID)
where T : Control
{
//Find the control.
Control foundControl;
if (control != null)
{
foundControl = control.FindControl(controlID);
if (foundControl != null)
{
//Return the Control as T.
return foundControl as T;
}
foreach (Control c in control.Controls)
{
foundControl = c.FindControlRecursive(controlID);
if (foundControl != null)
{
//Return the Control as T.
return foundControl as T;
}
}
}
return null;
}
Get the macro that has ID="headermenuMacro".
//Get the header menu macro (another usercontrol) which is also on this page. Macro menuMacro = this.Page.FindControlRecursive<Macro>("headermenuMacro");
On that you can do FindControl or FindControlRecursive to get the control that you need :).
It's an extension method so you don't need to use the first parameter :). If you want to use it as a normal method you need to do this:
//Get the header menu macro (another usercontrol) which is also on this page. Macro menuMacro =FindControlRecursive<Macro>(this.Page, "headermenuMacro");
Hmm. Still not getting anything (slightly out of my comfort zone...) Does this Recursive method account for the fact that the control is on a different master page? I have this master page structure:
On StandardPage.master, I have a macro with alias of "PageMessage" which points to a user control called "Message.ascx". All this has on it is an asp:Label, which I want to use as a generic control to provide the user with feedback.
On MyPage.master (which has StandardPage.master as its master page), I have a second macro which points to a different user control. From this control, I need to access the properties of the label on the first user control.
Hmm, with all respect to Jeroens great codesnippet, I would think it might be a better job for jQuery? Simply just make a javascript document which acts like the "connector" on control X's events to manipulate the attributes of the Y (your label)
var MyConnector = function() { function manipulateLabel() { // Parse HTML tree to find your label (span) control and manipulate it $('.yourLabel').text("Hello World"); }
@Bo jQuery could work, but that depends on what you need to do with the data.
@Mike If you debug is there anything in marco you get from FindControlRecursive? This should work:
//Get the header menu macro (another usercontrol) which is also on this page. umbraco.presentation.templateControls.Macro menuMacro = FindControlRecursive<umbraco.presentation.templateControls.Macro>(this.Page, "headermenuMacro");
True, Jeroen, however it seems like the label "just" acts as a generic response to any clients action. Honestly, I don't know what is more effecient, the generic recursive method or the javascript object :)
User control macro on master page - how to access its properties from another user control?
I have a "StandardPage" master page, and I've placed a macro on it which points to a user control "Message.ascx". Let's say this control contains an asp:Label control.
On a different master page, which uses StandardPage as *its* master page, I have another macro which points to another separate user control. I need this second control to be able to set the Text property of the asp:Label on the first user control.
Is this do-able?
Mike
Yes this is possible. You can fetch another usercontrol and do stuff with that. Here is a sample using macro's:
Recursive method to find the control:
Get the macro that has ID="headermenuMacro".
On that you can do FindControl or FindControlRecursive to get the control that you need :).
Jeroen
Thanks Jeroen, but the initial call to FindControlRecursive is failing because it needs two parameters - do I pass null in as the first parameter?
Mike
It's an extension method so you don't need to use the first parameter :). If you want to use it as a normal method you need to do this:
Jeroen
Hi Mike,
The method Jeroen pasted is an extension method, you can read everything about it here at Scott Gu's blog: http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx
So bascially you call would look something like: yourControl.FindControlRecursive("controlID"); where Type defines the type you're looking for.
Correct me if i'm wrong here, Jeroen.
/ Bo
Edit: Way ahead of me, Jeroen ;)
Hmm. Still not getting anything (slightly out of my comfort zone...) Does this Recursive method account for the fact that the control is on a different master page? I have this master page structure:
Master.master --> StandardPage.master --> MyPage.master
On StandardPage.master, I have a macro with alias of "PageMessage" which points to a user control called "Message.ascx". All this has on it is an asp:Label, which I want to use as a generic control to provide the user with feedback.
On MyPage.master (which has StandardPage.master as its master page), I have a second macro which points to a different user control. From this control, I need to access the properties of the label on the first user control.
Thanks
Mike
Hmm, with all respect to Jeroens great codesnippet, I would think it might be a better job for jQuery? Simply just make a javascript document which acts like the "connector" on control X's events to manipulate the attributes of the Y (your label)
Just a suggestion, really :)
Something along the lines this javascript object:
var MyConnector = function() {
function manipulateLabel() {
// Parse HTML tree to find your label (span) control and manipulate it
$('.yourLabel').text("Hello World");
}
return {
manipulateLabel : manipulateLabel
};
}();
From any event on Control X you can call this with: onclick="MyConnector.manipulateLabel();"
Edit: sorry for the code layout (or lack of it..), never been good friends with the code snippet here in out ;)
@Bo jQuery could work, but that depends on what you need to do with the data.
@Mike If you debug is there anything in marco you get from FindControlRecursive? This should work:
Remember that this method searches for an ID. Your macro doens't have this by default. You shouldn't search for the Alias which you normally set.
Jeroen
True, Jeroen, however it seems like the label "just" acts as a generic response to any clients action. Honestly, I don't know what is more effecient, the generic recursive method or the javascript object :)
Well, I'm obviously doing something wrong with your code, Jeroen, because I found this (very similar) post by Lee: http://www.blogfodder.co.uk/2010/12/3/access-site-wide-user-message-box-from-umbraco-macro and it works fine.
Label lblMessage = (Label)General.FindControlRecursive(Page, "lblMessage");
lblMessage.Text = "This is my message";
is working on a reply...