Which event is fired on master page after content pages have rendered - c#

I have a requirement where I need to pass the value of page title to Facebook share plugin only after the page title has been set by the content page. There are a few pages(Dynamic Data pages) where the page's title is set upon a specific control's PreRender event (I cannot change this).
If I pass the content page title on Master page's PreRenderit returns the unset value as Master page's PreRender is fired before content page's control PreRender. I tried passing the value in the master page's Unload event but it did not work! How do I achieve this?
So the question can be summarised as, perform something on master page after the content pages have performed all of their tasks and are ready to unload.
Is there a way to do this in master page itself or will I have to do this in the individual pages?
Reference: Events in ASP.NET Master and Content Pages
Dynamic Data Page:
string Location = null;
protected void DynamicFilter_PreRender(object sender, EventArgs e)
{
DynamicFilter Filter = (DynamicFilter)sender;
MetaColumn metaColumn = table.GetColumn(Filter.DataField);
QueryableFilterUserControl fuc1 = Filter.FilterTemplate as ForeignKeyFilter;
if (fuc1 != null && fuc1.FilterControl != null)
{
DropDownList ddl = fuc1.FindControl("DropDownList1") as DropDownList;
if (ddl != null)
{
if (metaColumn.DisplayName == "Location")
{
if (ddl.SelectedIndex != 0)
{
Location = ddl.SelectedItem.Text;
}
}
}
}
if (Location != null)
{
// set the page title based on the location
Page.Title = String.Format("Recent {0} Fares", Location);
}
}
This function sets the content page's(a dynamic data page) title.
Now I need to access this title on the my master page only after it has been set.
Master Page:
protected void Page_Load(object sender, EventArgs e)
{
// pass the value of page title to SocialNetworkingHelper class to do some work
SocialNetworkingHelper.SetSocialMediaMetaTag(this.Page, this.Page.Title);
}

use page unload event to perform final cleanup like closing files, releasing memory etc...if you set page title here it will not be rendered....you have to set title on control prerender event itself...

Related

How to find a master page list control using its id on master page iitself and add attribute to it? [duplicate]

I have a master page which contains a label for status messages. I need to set the status text from different .aspx pages. How can this be done from the content page?
public partial class Site : System.Web.UI.MasterPage
{
public string StatusNachricht
{
get
{
return lblStatus.Text;
}
set
{
lblStatus.Text = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
I have tried this, but was unsuccessful in making it work:
public partial class DatenAendern : System.Web.UI.Page
{
var master = Master as Site;
protected void Page_Load(object sender, EventArgs e)
{
if (master != null)
{
master.setStatusLabel("");
}
}
protected void grdBenutzer_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
//some code
if (master != null)
{
master.setStatusLabel("Passwort erfolgreich geändert.");
}
}
catch (Exception ex)
{
if (master != null)
{
master.setStatusLabel("Passwort konnte nicht geändert werden!");
}
}
}
}
}
In the MasterPage.cs file add the property of Label like this:
public string ErrorMessage
{
get
{
return lblMessage.Text;
}
set
{
lblMessage.Text = value;
}
}
On your aspx page, just below the Page Directive add this:
<%# Page Title="" Language="C#" MasterPageFile="Master Path Name"..... %>
<%# MasterType VirtualPath="Master Path Name" %> // Add this
And in your codebehind(aspx.cs) page you can then easily access the Label Property and set its text as required. Like this:
this.Master.ErrorMessage = "Your Error Message here";
In Content page you can access the label and set the text such as
Here 'lblStatus' is the your master page label ID
Label lblMasterStatus = (Label)Master.FindControl("lblStatus");
lblMasterStatus.Text = "Meaasage from content page";
It Works
To find master page controls on Child page
Label lbl_UserName = this.Master.FindControl("lbl_UserName") as Label;
lbl_UserName.Text = txtUsr.Text;
I have a helper method for this in my System.Web.UI.Page class
protected T FindControlFromMaster<T>(string name) where T : Control
{
MasterPage master = this.Master;
while (master != null)
{
T control = master.FindControl(name) as T;
if (control != null)
return control;
master = master.Master;
}
return null;
}
then you can access using below code.
Label lblStatus = FindControlFromMaster<Label>("lblStatus");
if(lblStatus!=null)
lblStatus.Text = "something";
You cannot use var in a field, only on local variables.
But even this won't work:
Site master = Master as Site;
Because you cannot use this in a field and Master as Site is the same as this.Master as Site. So just initialize the field from Page_Init when the page is fully initialized and you can use this:
Site master = null;
protected void Page_Init(object sender, EventArgs e)
{
master = this.Master as Site;
}
This is more complicated if you have a nested MasterPage. You need to first find the content control that contains the nested MasterPage, and then find the control on your nested MasterPage from that.
Crucial bit: Master.Master.
See here: http://forums.asp.net/t/1059255.aspx?Nested+master+pages+and+Master+FindControl
Example:
'Find the content control
Dim ct As ContentPlaceHolder = Me.Master.Master.FindControl("cphMain")
'now find controls inside that content
Dim lbtnSave As LinkButton = ct.FindControl("lbtnSave")
If you are trying to access an html element: this is an HTML Anchor...
My nav bar has items that are not list items (<li>) but rather html anchors (<a>)
See below: (This is the site master)
<nav class="mdl-navigation">
<a class="mdl-navigation__link" href="" runat="server" id="liHome">Home</a>
<a class="mdl-navigation__link" href="" runat="server" id="liDashboard">Dashboard</a>
</nav>
Now in your code behind for another page, for mine, it's the login page...
On PageLoad() define this:
HtmlAnchor lblMasterStatus = (HtmlAnchor)Master.FindControl("liHome");
lblMasterStatus.Visible =false;
HtmlAnchor lblMasterStatus1 = (HtmlAnchor)Master.FindControl("liDashboard");
lblMasterStatus1.Visible = false;
Now we have accessed the site masters controls, and have made them invisible on the login page.

Accessing Childpage Control Value from Master Page Button Click

I have a master page with a button and i have a client page with master page reference. I would like to change the child page label values while i click the master page button. I can't change that.
Label StrHref= (Label)MainContent.FindControl("lblhead");
HiddenField StrCalId = (HiddenField)MainContent.FindControl("hf_callid");
StrHref.Text = "12345678901";
StrCalId.Value = "1395741766.47";
Try the below code on your masterPage button click event.
I have kept one button on masterpage with name as btnMaster and on child page I have put on text box and on click of MasterPage btnMaster I have used the below method.
protected void btnMaster_Click(object sender, EventArgs e)
{
var textbox = this.ContentPlaceHolder1.FindControl("txtChild") as TextBox;
textbox.Text = "Text from Master to child control";
}
Let me know if you have any issue.

Accessing Other Page Controls From This Page

Page 1 - Ticket.aspx, DropDownList1, ModalPopUpextender with id mpe
Page 2 - Customer.aspx, btnSave
The index change event of dropdown will pop up mpe which has an iframe. This iframe loads Customer.aspx.
I am trying to access page1 controls in the button click event, but unable to.
Customer.aspx.cs:
protected void btnSave_Click()
{
Ticket page = new Ticket();
ModalPopUpExtender mpe = (ModalPopUpExtender)page.FindControl("mpe");
DropDownList ddl = (DropDownList)page.FindControl("DropDownList1");
//error here - Object reference not set to an instance
mpe.hide();
ddl.selectedindex=0;
}
Why is this not working. Using a Session variable should work right?
You may use Server.Transefer instead of Response.Redirect and then you can find the control in the current page.
Like:
TextBox tb = (TextBox)PreviousPage.FindControl("textbox1");
EDIT:
if (Page.PreviousPage != null)
{
DropDownList ddl1 =
(DropDownList)Page.PreviousPage.FindControl("DropDownList1");
if (ddl1 != null)
{
Label1.Text = ddl1.SelectedItem.Text; //your logic
}
}
What you are trying may not be doable from the server side, but it can be easily be done with a little javascript. Here is a link where you can get a working piece of code.
Hope this helps.

how to redirect to another page and move the content

I have a page with a lots of elements in it.
I want to let the user press a button and then page redirected and the contents of the div inserted into the HTML Editor which I have.
Thanks in advance
Add a public property to your source page. Add runat=server and an id to your div.
public String HtmlContent
{
get
{
return div.InnerHtml;
}
}
Add virtual path of your source page to your editor page.
<%# PreviousPageType VirtualPath="~/source.aspx" %>
You need to redirect from your source page with using Server.Trasfer method.
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("editor.aspx");
}
Finally in your editor page you can pass the values to your Html Editor.
if (PreviousPage != null)
{
string innerHtml = PreviousPage.HtmlContent;
}
You can achieve something like that with Cross-page posting in ASP.Net.
You may want to consider storing the data into a database and then just passing the id of the record to the new page as a query parameter; then on your redirected page, read the data out of the database using the id passed in

Trouble getting a master page control in a page that uses it

I have a Label in my mater page which i want to access in a page which uses the same mater page.
I tried..
string text = ((Label)Master.FindControl("myLabel")).Text; //Always returns empty string
P.S i have included <%# MasterType virtualpath="~/Masters/Master1.master" %>
still not working
As Waqas Raja mentioned in comments, the problem is in event sequence: master's Load event occurs after page's Load event. So you could just use Page.LoadComplete event in your page:
protected void Page_LoadComplete(object sender, EventArgs e)
{
string text = ((Label)Master.FindControl("myLabel")).Text;
}
and it should give you desired value of the textbox.
i thing you label in master page reside in content place holder. so
ContentPlaceHolder mpContentPlaceHolder =
(ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
string TextBoxvalue =
((TextBox) mpContentPlaceHolder.FindControl("TextBox1")).Text;

Categories

Resources