This project is read-only.

passing data between user controls

Topics: Developer Forum
Mar 20, 2009 at 1:20 AM

I'm new to N2.

I wonder if the following scenario is possible:

1) create a custom user control with 1 textbox and a botton; add this user control to a given page (using a "part"/"zone" - reading the documentation and code samples I now that's somwhow possible)
2) create a second custom control with a simple label; also add this user control to a given page
3) write something in the first control's text box and press the button; how to show the textbox's content in the label of the second user control? What is the best approach to do this?


Mar 20, 2009 at 10:02 AM
If you ask about an N2 specific way to do it (otherwise, any appropriate ASP.Net tactic should be fine), a possible scenario is this:
- create a ContentItem, which template (.aspx) will host both of your controls, this item will be accessible to both controls as .CurrentPage property
- create 2 other sub-items and assign them in your user controls as templates (stricly speaking, this step is unneccessary, because you only want to use page's content item; so instead of using this.CurrentPage,  you may well go with a more generic N2.Context.Current.UrlParser.CurrentPage as MyPageItem)

if done correctly, you'll be able to store fields in the first control:

//inside some post-back event handler in user control #1
this.CurrentPage["MyField"] = this.tbMyField1.Text; //put value to a parent page's content item
N2.Context.Persister.Save(this.CurrentPage);//persist ..

..then fetch them in the second:

//inside OnLoad in user control #2
this.tbMyField.Text = this.CurrentPage["MyField"] as string;