how to update content page from master page? - c#

Menu links are in the master page and and the corresponding click events are written in the master page itself. I have only one content page. Data is saved in html form in the DB and that will be rendered to the content page on corresponding menu link clicks. But the problem is content page load occurs before the master page so the content page is blank. Here my code-
public void GetLinkPage(Int32 linkId)//master page
{
LinkContentEnitity linkContent = new LinkContentEnitity();
linkContent = PageController.GetPageContent(linkId);
}
linkContent contains that content page in HTML form. How can I print this value on content page?

Though it's not entirely clear, it sounds like you are saying your content page needs information in its own Load event that is generated in the Page_Load event of the master page?
So either move the code that calls GetPageLink() to PreRender in your content page, or add an event handler in ContentPage for Page_LoadComplete() (which fires after all child controls on a page are loaded) and call GetPageLink() and do your rendering from there, e.g. in content page:
protected override void OnInit(EventArgs e)
{
Page.LoadComplete += new EventHandler(Page_LoadComplete);
}
protected void Page_LoadComplete(object sender, EventArgs e) {
// do stuff here, instead of OnLoad/Page_Load event
}
By the way this is a useful reference for event order. The problem you are having is very easy to get into when dealing with nested controls (master/content, usercontrols, etc), it helps to have a good understanding of the event order.
http://msdn.microsoft.com/en-us/library/dct97kc3.aspx

One way to do it might be this... On your content page, add a public method that does the work you need done. In the master page, cast this.Page to your page class and call that method.

Related

Is it possible to remove a user control (.ascx) present in master page (.master) from content (.aspx) page?

I have a master page which is being inherited to a lot of content page. I have one particular page in which I do NOT want one of the user controls present in the master page. I do NOT want to create a separate master page for this. I want to know if there's any way to prevent the user control present in master page being inherited to child / content page. Appreciate your help. thanks !
In the user control,
protected void Page_Init(object sender, EventArgs e)
{
Css.Register(this, "/css/reset.css", BrowserTarget.All, true);
}
In the master page, the user control is added
The part (css in reset.css) I'm trying to remove / prevent being inherited to
the content page
ol, ul {
list-style: none;
}
You can use in the child control:
((UserControl)this.Master.FindControl("userControlName")).Visible = false;
In order for the control to never exist at all with what you're saying and your feedback that hidden doesn't fit your needs, I'm guessing removing the control won't either.
The only other option here is to change the master page to have a flag that loads the control. This changes the approach from "remove this if" to "include this when." Then, in your calling pages, you'll need to set that flag. To make this new change less invasive, set the default the true and in this page set it to false.
Try this
MasterPage master = this.Master;
UserControl userControl = (UserControl)master.FindControl("userControlName");
if(userControl != null)
{
master.Controls.Remove(userControl );
}

Change label in masterpage in just one content page

How do I define a label in a masterpage from inside one control page, withouth losing it when I navigate to another control page? I know that I can use this code and it works:
(Master.FindControl("myControl") as Label).Text = "someNewContent";
But I have to define this on every page to keep the same content in the label. Is there a easier/shorter way to define this piece of code only one time in the whole program? Thanks in advance.
I think I get the gist of what you're asking:
Firstly, I would strongly type the master page, in your content page just below the #Page directive, by using the #MasterType directive:
<%# MasterType TypeName="*fully qualified type of your master page*" %>
Next, place a public property on your master page, like so:
public string MyText
{
set { this.ViewState["TheText"]; }
}
In your content page (during the Page_Init, for instance) you can add:
this.Master.MyText = "Whatever you want to say!";
Then load your master pages control text property in the Page_Load event:
protected void Page_Load(object sender, EventArgs e)
{
this.myControl.Text = Convert.ToString(this.ViewState["TheText"]);
}
This won't persist from content page to content page because each content page instantiates a new instance of the master page. In that case, put whatever text you want to persist in Session, then read it in the mater pages Page_Load event.
Hope this answers your question.

When in the page lifecycle we can assign master page?

I know ViewState is available between InitComplete and Preload events in LoadViewSate method. Similarly, I want to know in which page lifecycle event can we assign a master page for a particular page?
Because the master page and content page are merged during the
initialization stage of page processing, a master page must be
assigned before then. Typically, you assign a master page dynamically
during the PreInit stage
On Page PreInit event
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/MyMaster.master";
}
Read Working with ASP.NET Master Pages Programmatically
From: ASP.NET Page Life Cycle Overview
Page Event
Typical Use
PreInit
Raised after the start stage is complete and before the initialization stage begins. Use this event for the following:
Check the IsPostBack property to determine whether this is the first time the page is being processed. The IsCallback and IsCrossPagePostBack properties have also been set at this time.
Create or re-create dynamic controls.
Set a master page dynamically.
Set the Theme property dynamically.
Read or set profile property
values.
Note If the request is a postback, the values of the controls have not yet been restored from view state. If you set a control property at this stage, its value might be overwritten in the next event.
From: Attaching Master Pages Dynamically
In addition to specifying a master page declaratively (in the # Page directive or in the configuration file), you can attach a master page dynamically to a content page. Because the master page and content page are merged during the initialization stage of page processing, a master page must be assigned before then. Typically, you assign a master page dynamically during the PreInit stage, as in the following example:
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/DefaultMaster.master";
}
Edit:
Source: ASP.NET Master Pages - How Master Pages Work
You can use #Page directive also to specify master page.
<% # Page Language="C#" MasterPageFile="~/Master.master" Title="Content Page 1" %>

Activating a ASP Multiview control's view from other page without querystring/session

I was looking for an alternative that may be used to activate a multiview control of other page without passing a querystring/session variable.
Basically, my Home.aspx page has a link that takes us to a specific page say "NewPage.aspx". The NewPage.aspx page has a multiview control that has three child views.
I want to click on the link of the Home.aspx and go to NewPage.aspx with MultiView1.ActiveViewIndex=1. Please remember that I do not want to pass any querystring variable as that link already contains some encrypted data as querystring and adding another variable can cause the data to corrupt. Maintaining a session isn't a solution as well because the application is quite big.
Any inbuilt method that can activate that view? (I don't seem to be talking practical but any help is really appreciated)
If you are asking about how navigate to this page I would wire up a button event (I prefer to do so in OnInit). Something like this:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.btnlinkclick.Click += new EventHandler(btnlinkclick_Click);
}
void btnlinkclick_Click(object sender, EventArgs e)
{
this.MultiView1.ActiveViewIndex = 1;
}
This should work for you.

Calling a parent page code behind method from a child page code behind - ASP.NET 2.0 Page Inheritance model

What would be the best way to call a method in the code-behind of parent page from the code behind of the child page in the ASP.NET 2.0 Web Site model?
Scenario: User clicks a link in the parent page to view a data record's detail in child page (The parent and child are tow seperate pages). The user will modified the data in the client page. Once the child page has processed the update, the child page will close and I need to rebind the parent page to reflect the update.
I did something similar, but it was calling the parent page from a user control that was included in the parent page
if(Page.GetType().ToString().Contains("ASP.Default_aspx"))
{
MethodInfo MyMethod = this.Parent.TemplateControl.GetType().GetMethod("MyMethod");
MyMethod.Invoke(this.Page, null);
MyMethod = null;
}
UPDATE: I have to do this from the code behind because the child page closes after the data has been updated.
Easiest way would be to do the whole thing on one page using a multiview or some such thing.
Other then that, with javascript you can call
document.opener.location.href = "url";
to change the url on the parent page. You could just keep it the same, or stick stuff in the query string and muck with those values on Page_Load
If you're opening the child page in a modal pop-up, you can access window.returnValue on the parent page (via JavaScript) and then invoke a page refresh or an Ajaxy rebind call.
Check this page out for how to return value from modal pop-up and do something based on that value on parent page.
However, if you have the option, I'd get away from opening the edit form in a separate page. I'd put the edit form in a user control and show/hide it a la lightbox style.
Not sure if this is exactly what you want but you could have used cross page posting for this. This allows you to post back to the parent page from the child page with the advantage of having access to the child page's ViewState. To do this you set the PostBackUrl property of a button to the child page. In the parent page you can then access the PreviousPage property to retrieve values from the child page. So in the child page have a button such as:
<asp:Button ID="Button1" runat="server" Text="Update data"
PostBackUrl="~/Parent.aspx" onclick="Button1_Click" />
Then in the Parent.aspx Page_Load event:
protected void Page_Load(object sender, EventArgs e) {
if (IsCrossPagePostBack) {
Page prevPage = this.PreviousPage;
string myVal = prevPage.FindControl("myTextBox1").ToString();
string myVal2 = prevPage.FindControl("myTextBox2").ToString();
//etc
}
}

Categories