updating master page based on content page condition

Last post 01-09-2009 10:31 PM by nb123. 18 replies.

Sort Posts:

  • updating master page based on content page condition

    01-02-2009, 11:05 PM
    • Member
      280 point Member
    • nb123
    • Member since 07-04-2006, 11:31 AM
    • Posts 651

    i have a label control in master page which i want to get updated based on condition in the content page1.aspx.I have declared the public property as follows in master page to expose its text property.
     public string labelText
            {
                get { return label.Text; }
                set { label.Text = value; }
            }

    in my content page,i am checking the querystring under page_load event with (!IsPostBack) condition,if desired value is present in querystring,i update  the label text.like following

    if(!Page.IsPostBack)
    {
    if(queryStringValue == "something")
    {Master.labelText = "text -" + value1;}

    thanks a lot


    }


    I keep getting the null reference error  at  "set { label.Text = value; } ".What am i missing here?

  • Re: updating master page based on content page condition

    01-02-2009, 11:59 PM
    Answer
    • Member
      656 point Member
    • jignesh_login
    • Member since 03-27-2008, 10:53 AM
    • India
    • Posts 102

     hi,As i understand that you want to set the text of Master page Label from content page based on querystring.so u need to find the Master page label and then use the following function to find the Master page control in content page

     

    TextBox txtCatName = (TextBox)FindChildControl(ParentControl, "ControlName");.

    public Control FindChildControl(Control start, string id)

        {
            if (start != null)
            {
                Control foundControl;
                foundControl = start.FindControl(id);

                if (foundControl != null)
                {
                    return foundControl;
                }

                foreach (Control c in start.Controls)
                {
                    foundControl = FindChildControl(c, id);
                    if (foundControl != null)
                    {
                        return foundControl;
                    }
                }
            }
            return null;
        }

    Jignesh Yes

     

    Jignesh

    Arise, awake; and stop not till the goal is reached

  • Re: updating master page based on content page condition

    01-03-2009, 12:10 AM
    • Member
      280 point Member
    • nb123
    • Member since 07-04-2006, 11:31 AM
    • Posts 651

    Thanks for your input on this But why do i have to try finding the master page control?I have added @master Type directive in the content page and have exposed the text property of the label to avoid all the above.I can get the intellisense for that label control property present in master page from my  content page.I am following this article here and am not sure where i am messing up.

    http://www.asp.net/learn/master-pages/tutorial-06-cs.aspx

  • Re: updating master page based on content page condition

    01-03-2009, 12:48 AM
    Answer
    • Member
      418 point Member
    • vinkesh
    • Member since 05-15-2008, 6:34 AM
    • Posts 63

    Hi,

    Specify your master page class name in TypeName attribute of MasterType directive.

    <%@ MasterType TypeName="MasterPageClassName" %>

      Now you can access your public properties as this.Master.propertyname

     Also try by moving your code from page load event to PreRenderComplete event of page.

  • Re: updating master page based on content page condition

    01-03-2009, 1:52 AM
    Answer
    • Member
      16 point Member
    • mrkpradeep
    • Member since 01-02-2009, 2:52 PM
    • Posts 3

    masterpage.master

    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 100%">
                <tr>
                    <td style="height: 5px">
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
                </tr>
                <tr>
                    <td>
            <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
            </asp:contentplaceholder>
                    </td>
                </tr>
            </table>
        </div>
        </form>
    </body>
    </html>

    MasterPage.master.cs

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class MasterPage : System.Web.UI.MasterPage
    {
        public string labeltext
        {
            get
            {
                return Label1.Text;
            }
            set
            {
                Label1.Text = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
         
        }
    }

    ChildPage.aspx

    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ChildPage.aspx.cs" Inherits="ChildPage" Title="Untitled Page" %>
    <%@ MasterType TypeName="MasterPage" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    </asp:Content>

    ChildPage.aspx.cs

     

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class ChildPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            this.Master.labeltext = TextBox1.Text;
        }
    }
     

     

     

  • Re: updating master page based on content page condition

    01-03-2009, 2:37 AM
    • Member
      418 point Member
    • vinkesh
    • Member since 05-15-2008, 6:34 AM
    • Posts 63

     Hi,

                Change your master page name to MyMasterPage and it will work. Currently default .net class name for master page and your application master page class name are same which is creating issue.

     

  • Re: updating master page based on content page condition

    01-03-2009, 2:58 AM
    Answer
    • Participant
      863 point Participant
    • nitindhiman
    • Member since 12-27-2007, 4:41 PM
    • chandigarh, india
    • Posts 144

     hi,

    you have to find the control in the page. here is the same kind of thred..

    with code


            ContentPlaceHolder cp = (ContentPlaceHolder)Page.Form.FindControl("ContentPlaceHolder1");
            Panel p = (Panel )cp.FindControl("hidden1");
            Label l = (Label)p.FindControl("Label1");
            Label2.Text = l.Text;
            l.Text = "nitin"; 

    hope it helps. thnx..

    Nitin Dhiman....!!!!
    Please Mark Answer If Helped....!!!!
  • Re: updating master page based on content page condition

    01-03-2009, 3:06 AM
    • All-Star
      59,913 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 10:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    in ur content page do this

    HtmlForm  form1 = (HtmlForm)this.Master.FindControl("form1");

    Label Label1 = (Label)form1.FindControl("Label1");

    Label1.Text = "value";

  • Re: updating master page based on content page condition

    01-03-2009, 3:45 AM
    Answer
    • Participant
      863 point Participant
    • nitindhiman
    • Member since 12-27-2007, 4:41 PM
    • chandigarh, india
    • Posts 144

     oops sorry i forgot the like in last post

     

    http://forums.asp.net/t/1365836.aspx

    Nitin Dhiman....!!!!
    Please Mark Answer If Helped....!!!!
  • Re: updating master page based on content page condition

    01-03-2009, 11:55 AM
    • Member
      280 point Member
    • nb123
    • Member since 07-04-2006, 11:31 AM
    • Posts 651

    Thank you everyone for the input on this..

    My master page name is not the default master page name..so names have been different since beginning.i tried using typeName = "myMasterPage" (the class name for the master page) but it gave me the parse error saying 'unable to load the type "myMasterPage". So i changed it back to virtual path.I have also tried to move it to onPreRenderComplete event as suggested but getting the same null reference error at set { label.Text = value; } .

    finding the control on master page just looks like too much work,if there r other solution.So i am really hoping to make it work without going thru control finding route..

  • Re: updating master page based on content page condition

    01-03-2009, 12:04 PM
    • Member
      280 point Member
    • nb123
    • Member since 07-04-2006, 11:31 AM
    • Posts 651

    mrkpradeep,my code looks almost like yours..except that the name of my master page is seasonMaster.master and i am trying to set the label text in page_Load(also tried onPreRenderComplete) event.Though I am not able to use the typeName = 'seasonMaster" in my code,its giving me could not load type "seasonMaster",so i am using virtualPath instead and giving the path to the seasonMaster.master there.I don't know what do i need to change here to make 'typeName' work...weird thing is,i have another public "get" property(only) on master page for different control, which i am able to use without any error on content page.

  • Re: updating master page based on content page condition

    01-03-2009, 12:05 PM
    • All-Star
      59,913 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 10:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    Since master page and content page are 2 different things you will have to use findcontrol to find the control

  • Re: updating master page based on content page condition

    01-04-2009, 11:59 PM
    • Member
      280 point Member
    • nb123
    • Member since 07-04-2006, 11:31 AM
    • Posts 651

    ok,i am trying to find the label control inside the nested master page.My situation is almost like the  article at the link below.I can find outer and inner master page's controlplaceholder fine,but when i am trying to find the label control inside the inner controlplace holder,it comes out as null.I don't understand what i am doing wrong here.

    http://aspalliance.com/1737_CodeSnip_Find_Control_in_Nested_Master_Pages.all 

  • Re: updating master page based on content page condition

    01-06-2009, 1:30 PM
    • Member
      280 point Member
    • nb123
    • Member since 07-04-2006, 11:31 AM
    • Posts 651

    i am not able to resolve this yet..and i think one of the problem was,master page control id changes and it gets prefixed with something like ctl00_ctl00_contentPlaceHolderId_myControl.I tried to find the control using that but its coming null..I checked the 'source' and i don't see that control at all..so why my control is not appearing at all in source view?

  • Re: updating master page based on content page condition

    01-07-2009, 3:56 AM

    Hi nb123,

    I suggest you read jignesh_login's replied first, and then use UniqueID property to solve your problem, to know why, please check this link,

    http://blogs.telerik.com/atanaskorchev/posts/08-06-06/The_difference_between_ID_ClientID_and_UniqueID.aspx

    And please check this paragraph,

    You can use the UniqueID property to find any control no matter how deep it is nested in the control hierarchy. Just pass its value to the FindControl method of the Page.

     

     

    Hong-Gang Chen
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 2 (19 items) 1 2 Next >