How do I do cross page posting from a master page?

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
 
G

Guest

Not sure, but I can give you a wonderful clue that might help:

The master page is implemented as a User Control. Not sure how it is wired,
but that should give you a direction to head when you look for answers.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
A

Alan Silver

Not sure, but I can give you a wonderful clue that might help:
The master page is implemented as a User Control. Not sure how it is wired,
but that should give you a direction to head when you look for answers.

Hmm, maybe I didn't get enough sleep last night (as usual!!), but I
don't see what you're getting at. Even if it a user control, I still
need to be able to cast it to something that the compiler will know has
the property I added for the textbox. I don't see how a user control is
any better than a master page in that respect.

Please could you be more explicit? Thanks
 
C

Christopher Reed

Try adding the @MasterType and @PreviousPageType to your second ASPX page.
Then use this:

(TextBox)PreviousPage.Master.FindControl("txtFind")

Hope this helps!
 
A

Alan Silver

Try adding the @MasterType and @PreviousPageType to your second ASPX page.
Then use this:

(TextBox)PreviousPage.Master.FindControl("txtFind")

Hope this helps!

Thanks for the suggestion, but I don't think I can do this as I won't
know the PreviousPageType. As the search box is on the master page, it
appears on every page of the site, so the PreviousPageType could be any
page on the site.

Any other ideas? Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top