PDA

View Full Version : need help accessing label value from master page into the content page


sayso36
06-06-2008, 06:05 PM
hi

I have lovem master page file and lovec content page. I'm assigning some text to labelm control in master page and trying to access the same labelm value in the content page and assign it to a labelc in content page. However its giving me blank. The result should show "i'm labelm " after the equal sign.

Please help! Below is my code.

lovem.master
-------------
<%@ Master language="c#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<%@ import Namespace="System.Data.SqlClient"%>

<script language="c#" runat="server">

void Page_Load(object sender, System.EventArgs e)
{
labelm.Text="i'm labelm"+"";
}
</script>
<html>

<form runat="server">
<h1>Standard Header For All Pages</h1>

<asp:Label id="labelm" runat="server" />

<asp:ContentPlaceHolder id="CPH1" runat="server">
</asp:ContentPlaceHolder>

</form>
</html>

lovec.aspx
-----------
<%@ Page MasterPageFile="lovem.master" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<%@ import Namespace="System.Data.SqlClient"%>

<script language="c#" runat="server">
void Page_Load(object sender, System.EventArgs e)
{
Label mpLabelc = (Label) Page.Master.FindControl("labelm");
labelc.Text = "from content page = " + mpLabelc.Text;
}
</script>

<asp:Content ContentPlaceHolderId="CPH1" runat="server">
</br>
below is contentpage labelc</br>
<asp:Label id="labelc" runat="server"/>

</asp:Content>


Thank You So much!!!!

demtron
06-09-2008, 02:04 AM
The event sequence in an ASPX page is such that the page's Load happens before the master's Load. Therefore, you are grabbing the control value from the master before it has been assigned a value.

One option for solving this is to put the master label value assignment in the Init event of the master, which will happen before the Load event of the page. Then, the page Load will be able to pick up the value you want.

demtron
06-09-2008, 02:07 AM
Hope that helps!

sayso36
06-12-2008, 05:15 AM
Thank you so much demtron. It worked :)