Home page access control

I am using UserControl which is present on the main page. I need to access a master page control in a UserControl. I need your suggestions.

A script is a label that appears on the home page. Based on the selection in usercontrol, I need to change the label of the homepage. UserControl is present on master page not in content holder.

+1


a source to share


2 answers


Create a public method (or public property) on the master page to change your label and in the UserControl, which you can call via an object Page.master

:



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

      

+4


a source


A quick and easy way is to create an event in control and process it 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 the name of the label on the master page.

test1 is the control on the homepage.

+1


a source







All Articles