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;
Related
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...
I am trying to connect an asp link button on a master page to its click event on the content page. I followed descriptions from various online posts, but when I try running the page, I get a Stack-overflow exception in the get{} set{} part of the link button property. I could not find anyone else with this problem, mainly because I don't know what the actual problem is. Here are some bits of my code:
Master Page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="mySite.master.cs" Inherits="mySite1.mySite" %>
<asp:LinkButton id="myLink" Text="Home" runat="server"/>
Master Page CS:
public LinkButton myLink
{
get
{
return myLink; //FAILS HERE IF I COMMENT OUT THE SET.
}
set
{
myLink = value; //THIS IS WHERE IT FAILS!
}
}
Index.aspx.cs:
protected void Page_Init(object sender, EventArgs e)
{
if(Master.myLink != null)
Master.myLink.Click += new EventHandler(MainTabBtn_Click);
}
protected void MainTabBtn_Click(object sender, EventArgs e)
{
//DO STUFF
}
Index.aspx:
<%# Page Title="MySite" Language="C#" MasterPageFile="~/MySite.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="MySite1.Index" %>
<%# MasterType VirtualPath="~/MySite.Master" %>
Anyone can tell from this what the problem is? Thanks for your help,
Marvin
You assign a value to the myLink property which assigns it to the myLink property (myLink = value) which assigns it to the myLink property... in an infinite loop - the classic stack overflow exception. Find the control on the master page instead.
Did you try this?
protected void Page_Init(object sender, EventArgs e)
{
LinkButton myLink = (LinkButton)Master.FindControl("myLink");
myLink.Click += new EventHandler(MainTabBtn_Click);
}
You should change
public LinkButton myLink
to
public LinkButton MyLink
Change the property name or the intern variable it is containing, any of those will be fine, because other way you will be calling the property myLink, that the value is myLink, that the value is myLink, etc etc
I'm trying to pass a value from a User Control to a code behind without luck. I can write the correct value (IDuser_uc) on the aspx page but I can't pass it to the code behind. Any tips?
User Control
protected void FormView_IDuser_DataBound(object sender, EventArgs e)
{
Label IDuserText = FormView_IDuser.FindControl("IDuserLabel") as Label;
IDuser_uc = Convert.ToString(IDuserText.Text);
}
ASPX page
<uc4:IDuser id="IDuser" runat="server" />
<% Response.Write(IDuser.IDuser_uc); // Here correct value %>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource_userConnections.SelectParameters["IDuser_par"].DefaultValue = IDuser.IDuser_uc ; // Here NO value
Response.Write("Connections :" + IDuser.IDuser_uc); // Here NO value
}
UPDATE
The problem was due to the fact that the User Control is run after the PageLoad which explains why I got an empty string. To solve the problem I used Page_PreRender instead of Page_Load on the code behind page.
Create a public method in your user control. When you post back the page have the Parent.aspx page call the user control method. Example....
In your User Control:
Public string GetUserControlValue()
{
return Label IDuserText;
}
In your Parent ASPX Pgae
var UserControlString = IDuser_uc.GetUserControlValue();
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.
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