Content page access to MasterPage control values (button values)

Last post 07-24-2008 2:57 AM by vrparekh@gmail.com. 11 replies.

Sort Posts:

  • Content page access to MasterPage control values (button values)

    06-09-2008, 10:50 AM
    • Participant
      824 point Participant
    • zoltac007
    • Member since 04-06-2006, 4:25 PM
    • Posts 1,554

    In the code behind for my master page I have several buttons, all of which set the value of a string value 'CatGroup'.  I want to access this value from my content page.  Are there any tutorials that explain this activity.  I have read about Delegates, registering, etc, etc and am now totally confused.  Would greatly appreciate links to any straight forward newbie-like directions.  

    public partial class MasterPage001 : System.Web.UI.MasterPage
    {
    public string CatGroup;
    protected void Page_Load(object sender, EventArgs e)
    {
    Label1.Text = CatGroup;
    }
    protected void Button1_Click(object sender, EventArgs e)
    { CatGroup =
    "1"; }
    protected void Button2_Click(object sender, EventArgs e)
    { CatGroup =
    "2"; }

     Want to use the value in a Content page as:

    string FilterCode = CatGroup;

    Thank you.

  • Re: MasterPage buttons?

    06-09-2008, 11:53 AM
    • Participant
      1,134 point Participant
    • mihirpathak
    • Member since 10-18-2002, 4:16 AM
    • Posts 199

    Here one of my favorite article about Master Pages ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps

    MP

    I never desire to converse with a man who has written more than he has
    read. -Samuel Johnson, lexicographer (1709-1784)
  • Re: MasterPage buttons?

    06-09-2008, 12:09 PM
    • Participant
      824 point Participant
    • zoltac007
    • Member since 04-06-2006, 4:25 PM
    • Posts 1,554

    Thank you, I read that one and many of the others found on google.  I am looking for something more specific to this task.  The concept information discussions by their nature speak in very general terms with constant references to "be careful when using . . . . " 

  • Re: MasterPage buttons?

    06-10-2008, 6:02 PM
    • Participant
      824 point Participant
    • zoltac007
    • Member since 04-06-2006, 4:25 PM
    • Posts 1,554

    I have found a bunch of information about accessing MasterPage controls and their propertied but I am trying to find out how to either listen to masterpage click event or set a value in a content page based on an action on the masterpage.  i.e. - when the client selects a masterpage button control I need to set a value in a content page.  It is my underrstanding that the master page is 'wrapped' around the content page after it is formed.

     

  • Re: MasterPage buttons?

    06-10-2008, 9:00 PM
    • Participant
      824 point Participant
    • zoltac007
    • Member since 04-06-2006, 4:25 PM
    • Posts 1,554

    This page clearly explain the procedure: http://aspnetlibrary.com/articledetails.aspx?article=Access-Master-Page-controls-from-the-Content-Page

    if using C# suggest converting all sample code first.

  • Re: MasterPage buttons?

    06-10-2008, 9:43 PM
    • Participant
      824 point Participant
    • zoltac007
    • Member since 04-06-2006, 4:25 PM
    • Posts 1,554

    Code below allows Content page to access a Text value based on which MasterPage button is selected by a user. Problem is the access appears to delay by one click.  ANY ideas?

    MASTERPAGE has six button controls, when one is selected the value is accessed by the content page and displayed in 'tbxCategoryCode.Text' on the content page DEFAULT.aspx.cs. 

    MASTERPAGE.master.cs *************************************

    public partial class MasterPage2008 : System.Web.UI.MasterPage
    {
        public int CatGroup = 1; 

        public string CatFilter
        {   get { return TextBox1.Text; }     }

        protected void Page_Load(object sender, EventArgs e)
        {   TextBox1.Text = Convert.ToString(CatGroup);        }

        protected void Button1_Click(object sender, EventArgs e)
        {   CatGroup = 1;
            TextBox1.Text = Convert.ToString(CatGroup);    }

        protected void  Button2_Click(object sender, EventArgs e)
        {   CatGroup = 2;
            TextBox1.Text = Convert.ToString(CatGroup);    }
      
       etc. etc.

    DEFAULT.aspx.cs  **********************************************************
     
    protected void Page_Load(object sender, EventArgs e)
        {
            CategoryFilterSelect = ((MasterPage2008)Page.Master).CatFilter;

            CategoryFilter.Text = Convert.ToString(CategoryFilterSelect);
            tbxCategoryCode.Text = CategoryFilter.Text;

  • Re: MasterPage buttons? Have to click twice.

    06-11-2008, 12:48 AM
    • Participant
      824 point Participant
    • zoltac007
    • Member since 04-06-2006, 4:25 PM
    • Posts 1,554

    ANYONE have any thoughts - I have to click the buttons twice on my masterpage to get the respective value on the content page?  I have tired locating call to controls into PreRender on both the masterpage and content page code behind, tried everyform of casting I know which are not many.  I can't believe that getting a simple value from a masterpage has to be this difficult.  Is there some other navigation bar that I should be using?  All I need to do is pass a text value of 1, 2, 3, 4, 5, 6, 7 or 8 to my content page when the client selects a button on the masterpage.  I am using this value is a ultimately a parameter to a ListBox stored procedure datasource.  I almost want to go back to the days of inc files.  HELP - deadline is here.

  • Re: Content page access to MasterPage control values (button values)

    06-11-2008, 12:50 AM

    Hi,

    You can try to expose a property in the master page's codebehind, then you can access it in the content page like a mastepage's property, below the sample code:

    1. Try to expose the property in the master page's codebehind:

      public string _catGroup;

       public string CatGroup
       {
           get
           {
               return _catGroup;
           }
           set
           {
               _catGroup = value;
           }
       }

    protected void Page_Load(object sender, EventArgs e)
    {
    Label1.Text = _catGroup;
    }
    protected void Button1_Click(object sender, EventArgs e)
    { _catGroup=
    "1"; }
    protected void Button2_Click(object sender, EventArgs e)
    { CatGroup =
    "2"; }

    2. Add the masterType directive for the content page:

    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" Title="Untitled Page" %>
    <%@ MasterType VirtualPath="~/MasterPage.master" %>

    3. The you can access the CatGroup in the content page like this:

    string FilterCode = this.Master.CatGroup;

    Hope it helps.


     

    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Content page access to MasterPage control values (button values)

    06-11-2008, 1:23 AM
    • Participant
      824 point Participant
    • zoltac007
    • Member since 04-06-2006, 4:25 PM
    • Posts 1,554
    Tried the code and can't see the masterpage label, not getting any response in content page, tbxCatMenuCode.Text remains blank.  I have no idea what is holding this up.  tbxCatMenuCode.Text is the content page control used as the SQL DataSource parameter.  I do not understand casting enough to properly cast this sequence but I think that may be why I am not seeing it.  The Label1 control is not visible on the masterpage and the control page tbxCatMenuCode text box remain empty.  There are no compile errors.

    <%@ Page Language="C#" MasterPageFile="~/MasterPage2008.master" AutoEventWireup="true" Debug="true" CodeFile="DefaultH.aspx.cs" Inherits="HE_DefaultH" Title="Untitled Page" Theme="Theme1" EnableEventValidation="false" %>

    <%@ MasterType VirtualPath="~/MasterPage2008.master" %>

    MASTERPAGE 

    public partial class MasterPage2008 : System.Web.UI.MasterPage
    {
    public string _catGroup;
    public string CatGroup
    {
    get { return Label1.Text; }
    set { _catGroup = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
         if (!Page.IsPostBack)
             {
                _catGroup =
    "1";
            }
               Label1.Text = _catGroup; 
    }
    protected void Button1_Click(object sender, EventArgs e)
    { _catGroup =
    "1"; }
    protected void Button2_Click(object sender, EventArgs e)
    { _catGroup =
    "2"; }
    protected void Button3_Click(object sender, EventArgs e)
    { _catGroup = "3"; }
    protected void Button4_Click(object sender, EventArgs e)
    { _catGroup =
    "4"; }
    protected void Button5_Click(object sender, EventArgs e)
    { _catGroup =
    "5"; }
    protected void Button6_Click(object sender, EventArgs e)
    { _catGroup =
    "6"; }
    protected void Button7_Click(object sender, EventArgs e)
    { _catGroup =
    "7"; }
    protected void Button8_Click(object sender, EventArgs e)
    { _catGroup =
    "8"; }

    CONTENT PAGE

    string
    CategoryFilterSelect;
    protected void Page_Load(object sender, EventArgs e)
    {
       CategoryFilterSelect =
    this.Master.CatGroup;
       tbxCatMenuCode.Text =
    Convert.ToString(CategoryFilterSelect);
     
       if (!Page.IsPostBack)
          {
              tbxCatMenuCode.Text =
    "1";
          }
            Label1.Text = _catGroup;
    }

    ADDED postback trap - label displays initial value then goes blank when any button except #1 is selected twice.

  • Re: Content page access to MasterPage control values (button values)

    06-11-2008, 2:44 AM
    Answer

    Hi,

    Try to refer the below code:

    public partial class MasterPage2008 : System.Web.UI.MasterPage
    {
    //public string _catGroup;
    public string CatGroup
    {
    get { return Label1.Text; }
    set { Label1.Text= value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
         if (!Page.IsPostBack)
             {
               // _catGroup =
    "1";

                Label1.Text = _catGroup; 
            }
              // Label1.Text = _catGroup; 
    }
    protected void Button1_Click(object sender, EventArgs e)
    { Label1.Text =
    "1"; }
    protected void Button2_Click(object sender, EventArgs e)
    { Label1.Text =
    "2"; }
    protected void Button3_Click(object sender, EventArgs e)
    { Label1.Text = "3"; }
    protected void Button4_Click(object sender, EventArgs e)
    { Label1.Text =
    "4"; }
    protected void Button5_Click(object sender, EventArgs e)
    { Label1.Text =
    "5"; }
    protected void Button6_Click(object sender, EventArgs e)
    { Label1.Text =
    "6"; }
    protected void Button7_Click(object sender, EventArgs e)
    { Label1.Text =
    "7"; }
    protected void Button8_Click(object sender, EventArgs e)
    { Label1.Text =
    "8"; }

    CONTENT PAGE

    string
    CategoryFilterSelect;
    protected void Page_Load(object sender, EventArgs e)
    {
       CategoryFilterSelect =
    this.Master.CatGroup;
       tbxCatMenuCode.Text =
    Convert.ToString(CategoryFilterSelect);
     
       if (!Page.IsPostBack)
          {
              tbxCatMenuCode.Text =
    "1";
          }
           // Label1.Text = _catGroup;

     Label1.Text = CategoryFilterSelect ;
    }

    Hope it helps.

    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Content page access to MasterPage control values (button values)

    06-11-2008, 3:27 AM
    • Participant
      824 point Participant
    • zoltac007
    • Member since 04-06-2006, 4:25 PM
    • Posts 1,554

    Thank you for the follow through - I am 22 hours into this work day, going to take a break - will try new code tomorrow.  Thank you very much for the repsonse.  At least I think I understand the changes.

  • Re: Content page access to MasterPage control values (button values)

    07-24-2008, 2:57 AM

    do one think take one hiddenfield, and write javascript on button's onclientclick event , and change the hiddenfield's value,

    now in contentpage's pageload event or any other event where you want it, just take the hiddenfield' s value...

    VISHAL PAREKH
Page 1 of 1 (12 items)