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'.
CodePudding user response:
This should work:
protected void Page_Load(object sender, EventArgs e)
{
string pageName = MainContent.Page.GetType().FullName;
if (pageName == "ASP.default_aspx")
{
// get text box 1 on child page.
TextBox tbox1 = (TextBox)MainContent.FindControl("TextBox1");
tbox1.Text = "Hello world";
}
}
I am assuming that the content control in master page is thus this:
<div >
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
<hr />
<footer>
<p>© <%: DateTime.Now.Year %> - My ASP.NET Application</p>
</footer>
</div>
And thus in the child page "default.aspx", then I assumed this:
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
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:
protected void Page_Load(object sender, EventArgs e)
{
// this is child page load event, change a
// change TextBox1 that is in master page
TextBox tb1 = (TextBox)Page.Master.FindControl("TextBox1");
tb1.Text = "hello world";
}
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:
if (Session["Projectinfo"] = null
{
// no project information passed, send user back to
// my projects page
response.redirect("MyProjects.aspx")
}
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.
CodePudding user response:
This is the only way I believe. I don't think you can actually change the literal contentplaceholder from the masterpage.
protected void btn_search_Click(object sender, ImageClickEventArgs e)
{
string pageName = this.ContentPlaceHolder1.Page.GetType().FullName;
if (pageName != "ASP.chinashipmanagerhome_aspx")
{
string t = Base64Encode(tb_search.Text);
Response.Redirect("ChinaShipManagerHome.aspx?search2=" t); //Redirect with query
}
Then in the 'Page_Load' I added, after checking that the person has not lost session. I also made sure that its not a post-back.
else
{
if (Request.QueryString["search2"] != null)
{
tb_search.Text = Base64Decode(Request.QueryString["search2"].ToString());
btn_search_Click(this, new ImageClickEventArgs(1, 1));
}
}
Here it recalls 'btn_search_click' but now its on the right page.