Accessing TextBox control half way through nested MasterPages

vorkarrorbBem

New Member
I have four master pages which can reach three nested levels deep:
  • Main.master
  • CommonHeaderAndFooter.master
  • FullWidth.master / LeftNav.master
On \[code\]CommonHeaderAndFooter.master\[/code\] I have:\[code\]<asp:TextBox runat="server" ID="SearchTextBox" /><asp:Button runat="server" ID="SearchButton" PostBackUrl="~/search-results" Text="<%$ Resources:Common,Search %>" />\[/code\]In Search-Results (whose master page \[code\]FullWidth.master\[/code\]) I have:\[code\]if ((PreviousPage != null && PreviousPage.IsCrossPagePostBack) || Page.IsPostBack){ Response.Write("cross page"); string searchQuery = ...}else{ Response.Write("normal hit!");}\[/code\]By now you've probably guessed what I want to do, I need to populate \[code\]searchQuery\[/code\] with the value of \[code\]SearchTextBox\[/code\]. The only way I can find to access the control (from a page also using \[code\]FullWidth.master\[/code\]) is to do\[code\]((TextBox) Master.Master.Master.FindControl("CphMain").FindControl("txtSearch2")).Text;\[/code\]However, this will immediately break when I start to use \[code\]PreviousPage\[/code\] as the previous page could be any number of nested masterpages deep. I thought about writing a recursive function which I have seen elsewhere, looking for all the controls in a master page. However, I seem to have to use \[code\]FindControl("CphMain")\[/code\], it does not appear in the master page controls collection (\[code\]MasterPage.Controls\[/code\]), making any kind of recursive control impossible.I then thought about using a less ASP.NET method and simply checking for a postback (either kind) and then looking at \[code\]Request.Form\[/code\]. However, because of the nesting the control does not have a traditional \[code\]name\[/code\] attribute and therefore cannot be found.If nothing else, this seems a lot harder than it should be (or I'm making it so), any solutions?
 
Top