I have seen the other way around, where coders need to change the master page for a particular content. But in this case, I am more worried about what is in the content, and I wish to change it.
protected void btn_search_Click(object sender, ImageClickEventArgs e)
{
string pageName = this.ContentPlaceHolder1.Page.GetType().FullName;
if (pageName != "ASP.ChinaShipManagerHome_aspx")
{
// then change it here?!
}
}
As you can surmise in my case, I have a button "Search" on my master page. However, they may have strayed away from the content, that the ‘search’ needs to interact with. So when they click ‘search’ it needs to switch back to the right ‘Content’ in ‘ContentPlaceHolder1’.
2
Answers
This is the only way I believe. I don't think you can actually change the literal contentplaceholder from the masterpage.
Then in the 'Page_Load' I added, after checking that the person has not lost session. EDIT: It actually does not matter if its postback, in fact it has to be both.
Here it recalls 'btn_search_click' but now its on the right page. EDIT: Must also check tb_search.Text is empty, or you will get a circular reference when searching again.
This should work:
I am assuming that the content control in master page is thus this:
And thus in the child page "default.aspx", then I assumed this:
so, we can/should be able to use FindControl on that "main content" as per above.
Edit:
However, it looks like the poster wants to change master page from child page.
Ok, then assuming above, and our text box is on the master page, and we want to change from child page?
Sure, then this:
Edit#2: how to re-direct to another page
In many cases, I will have say users navagte to a page, but that is NOT the page I want.
For example, I have a project page, and we can ONLY get to that project page from MyProjects.
so, to deal with above, we do NOT put such code in the master code, but such code goes in the child page.
So, for example, if we say typed in the Project.aspx page (it is a child of master), but I want to "change" what page the user gets?
I’ll put such code in the on-load even of the child page.
say:
so, as a general rule, you don’t want to put such code in the master page, since then the master page code will over time becomes a big mess, since you now putting logic in the master page that in 99% of cases belongs in the given page code.
However, such code CAN go in the master page, and to display a different page, we don’t attempt/try to swap out the content page, but in fact just do a plain jane navagate to the page we want, and that page being a child page of master will thus continue to display the master page, and having jumped to the correct child page, then all is well.
So, we don’t need to "change" what content template gets displayed, but simple navigate to that child page in question with a simple response.redirect. Since that child page going to display the master page anyway, then we just achieved the same goal as swapping out/changing what child page we are going to display anyway, right?
In other words, a simple response.redirect should suffice to "change" what child page we are to display.
Edit#3: button search is in/on the menu bar (master page).
As I have repeated stated, we really do NOT care WHERE the search and button is.
The question REALLY is this:
I have a text box, and a button. User will type in some search critera, and click the button. I then want to navagate to a page to display the search results.
In fact, this is the MOST basic web 101 question on the planet.
In such cases, and SUPER common? We are to take that search critera and pass it to the target page. I mean, how does ANY working web site have a button, take user input, and then navagate to the next page in the applcation? As noted, then we are to pass that search critera to the "search results/display" page.
So, all is lost here if we can’t have simple pass some value(s) to the next web page in qeustion – 100% of all applications require this SUPER SIMPLE skill set.
So, lets write the code to take the user input, have user click a button, and then navagate to a page to display the results.
It does NOT matter if that text box and button are on ANY web page, and it does not matter if the text box and button are on the master page.
and it does not matter if the text box and button are on the menu bar in the master page.
In all cases, the question remains the same SUPER SIMPLE question:
How can I have a text box, and button click show the search results of that text box, and button click.
So, lets assume this menu bar – we can be on ANY page in the application – we don’t care, but MORE important, we don’t have to care!!!
So, on the master page, this search could be in teh menu bar, or on the master page – don’t care, don’t matter.
In fact, this text box and search could be ANY PLACE in the WHOLE appliation.
After all, as noted, the MOST simple thing in a web application is:
I have a button, some text, and I need a button click to pass htis information to the next page in question!!!!
So, lets add this text box + search button to the master page.
Like this:
The code behind for the above button in the master page is this:
Again, the above is something we all do all day long – pass some values to another target page. We could use queryparms in the URL.
And now lets create the searchResults.aspx page. It can be say a gridview of results.
So, say a simple gv like thiS
And the code for this simple page with gv "search results" is thus this:
And thus now we can be on ANY page, type in the search, and hit search button.
we get this effect:
As above shows, it don’t matter what page, or where we are.
the simple goal is to take that input, and navagate to the search results page.
As I stated OVER AND OVER like a broken record, a simple navigate to the search results page is REALLY much the same as attempting to swap out the child page based on the current page we are on.
so, no real need to swap out the child page current displayed in the current master page – a simple navigate to page to display the search results is all we need here. So, as long as the search results page also has a master page – then we should be all happy and warm.
Edit#4: Lets not use session then, ok?
So, the button on the master page becomes this:
And now in our search result page, we have this:
It still works. I don’t think some worry about "session" changes this problem at all.
At the end of the day, we have a text box, and a button to search. The concpet of passing values to a page in asp.net? Gee, every application I ever written does that all the time.
in other words, we are RIGHT back to the most simple concpect.
Quite much "any" web site will have boatloads of example code that takes some user input, and then "moves on" to the next page, and passing of values to that next page has multiple solutions – you might use session, might use URL paramaters.
Or, as above shows, grab the value from the master page and use that value.