A
Alan Silver
Hello,
I would like to add a search text box to the master page, so that it
appears on every page of the site. I then want the button next to the
search box to do a cross page post to the search page, where I want to
pick up the contents of the search box.
Trouble is, I can't see how to get at the text box on the master page.
My code follows:-
This is on the master page's .master file...
<asp:TextBox ID="txtFind" RunAt="server" />
<asp:Button ID="btnFind" PostBackUrl="Search.aspx" Text="Find"
RunAt="server" />
and the code-behind for the master contains...
public partial class MasterPage : System.Web.UI.MasterPage {
public string searchKeywords {
get {return txtFind.Text;}
}
}
The Search.aspx.cs file contains this...
public partial class Search : Page {
public void Page_Load(Object o, EventArgs e) {
if (!IsPostBack) {
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack) {
MasterPage masterPage = (MasterPage)PreviousPage;
if (masterPage != null) {
// we're in business, do the search
}
}
}
}
}
However I get a compilation error "Cannot convert type
'System.Web.UI.Page' to 'System.Web.UI.MasterPage'" on the line...
MasterPage masterPage = (MasterPage)PreviousPage;
Any ideas? TIA
I would like to add a search text box to the master page, so that it
appears on every page of the site. I then want the button next to the
search box to do a cross page post to the search page, where I want to
pick up the contents of the search box.
Trouble is, I can't see how to get at the text box on the master page.
My code follows:-
This is on the master page's .master file...
<asp:TextBox ID="txtFind" RunAt="server" />
<asp:Button ID="btnFind" PostBackUrl="Search.aspx" Text="Find"
RunAt="server" />
and the code-behind for the master contains...
public partial class MasterPage : System.Web.UI.MasterPage {
public string searchKeywords {
get {return txtFind.Text;}
}
}
The Search.aspx.cs file contains this...
public partial class Search : Page {
public void Page_Load(Object o, EventArgs e) {
if (!IsPostBack) {
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack) {
MasterPage masterPage = (MasterPage)PreviousPage;
if (masterPage != null) {
// we're in business, do the search
}
}
}
}
}
However I get a compilation error "Cannot convert type
'System.Web.UI.Page' to 'System.Web.UI.MasterPage'" on the line...
MasterPage masterPage = (MasterPage)PreviousPage;
Any ideas? TIA