views:

695

answers:

3

I am using a UserControl Which is present in the Master Page. I need to access a Master page control in the UserControl. I need your suggestions.

The Scenario is A label is present in the Master Page. Based upon selections in the usercontrol i need to modify the masterpage label. The UserControl is present in the Master page itself not in the content place holder.

+1  A: 

Quick and Easy way is to create event in control and handle in master like this:

//Control aspx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" 
  Inherits="TestControl" %>

<div style="width:300px;border:2px groove blue;">
    <asp:Button ID="btn1" runat="server" Text="One" onclick="btn_Click" />
    <asp:Button ID="btn2" runat="server" Text="Two" onclick="btn_Click" />
    <asp:Button ID="btn3" runat="server" Text="Three" onclick="btn_Click" />
    <asp:Button ID="btn4" runat="server" Text="Four" onclick="btn_Click" />
</div>    

//Control C#

namespace Controls
{
    public partial class TestControl : System.Web.UI.UserControl
    {
        public delegate void UserChoice(TestEventArgs e);
        public event UserChoice OnUserChoice;

        protected void btn_Click(object sender, EventArgs e)
        {
            if (OnUserChoice != null)
                OnUserChoice(new TestEventArgs(((Button)sender).Text));
        }
    }

    public class TestEventArgs : EventArgs
    {
        private string _value;

        public TestEventArgs(string str)
        {
            _value = str;
        }
        public string Message
        {
            get { return _value; }
        }
    }
}


//MasterPage Code

protected void Page_Load(object sender, EventArgs e)
{
     test1.OnUserChoice += new 
        Controls.TestControl.UserChoice(test1_OnUserChoice);
}

void test1_OnUserChoice(ROMS.Intranet.Controls.TestEventArgs e)
{
    MasterLabel.Text = e.Message;
}

MasterLabel is name of the label in master page.

test1 is the control in master page.

TheVillageIdiot
+2  A: 

Create a public method (or public property) in the master page to modify your label and in the UserControl you are able to call it, through the Page.master object:

YourMasterPageClass master = Page.master as YourMasterPageClass;
if(master != null)
{
    master.YourEditMethod("hello");
}
Davide Vosti
A: 

In asp.net 2008:

All the controls in the master page is placed in the contentplace holder, so we have to refer the placeholder first then the control.

ContentPlaceHolder mpContentPlaceHolder;
ImageButton mp;
mpContentPlaceHolder =
    (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if (mpContentPlaceHolder != null)
{
    mp = (ImageButton)mpContentPlaceHolder.FindControl("imgbtHome");
    mp.Visible = false;
}

// ContentPlaceHolder1 is the id of place holder in which your control is placed.
// imgbtHome is the control you want to make in invisible

In asp.net2005:

ImageButton mp =(ImageButton)Master.FindControl("imgbtHome");
mp.visible=false;
Vibin Varghese