1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Interoperation between custom controls

Interoperation between custom controls

2 posts, 0 answered
  1. Vladislav
    Vladislav avatar
    1 posts
    Registered:
    05 Jul 2008
    26 Apr 2011
    Link to this post
    Hi! I use ASP.NET Server Control Library to develop custom controls for sitefinity.
    It looks like this:

    Both controls are inherit from SimpleView and use LayoutTemplateName from "Resources.Views". This works fine.
    Control1 has a Label control:
            protected virtual Label Label1
            {
                get
                {
                    return base.Container.GetControl<Label>("Label1", true);
                }
            }
    Control2 has TextBox and Button controls:
            protected virtual TextBox TextBox1
            {
                get
                {
                    return base.Container.GetControl<TextBox>("TextBox1", true);
                }
            }
            protected virtual Button Button1
            {
                get
                {
                    return base.Container.GetControl<Button>("Button1", true);
                }
            }

    Please, explain me how can I interop between these controls. For example, on Control2.Button1.Click Control1.Label1.Text should be changed to value of Control2.TextBox1.Text.

    I do next,
    On Control2.cs:
        public delegate void Control2ChangedEventHandler(object sender, Control2ChangedEventArgs e);
        public class Control2ChangedEventArgs : EventArgs
        {
            private string _text;
            public Control2ChangedEventArgs(string text)
            {
                _text = text;
            }
            public string ChangedText
            {
                get { return _text; }
            }
        }
        public event Control2ChangedEventHandler Changed;
        protected override void InitializeControls(GenericContainer container)
        {
            this.Button1.Click += new EventHandler(Button1_Click);
        }
        void Button1_Click(object sender, EventArgs e)
        {
            if (Changed != null) Changed(sender, new Control2ChangedEventArgs(this.TextBox1.Text));
        }

    On Control1.cs:
       // Declaration of Сontrol2
       protected Control2 _control2;
            protected override void InitializeControls(GenericContainer container)
            {
                this._control2 = (Control2)this.Page.FindControl("Control2");
                this._control2.Changed += new Control2ChangedEventHandler(_control2_Changed);
            }

            void _control2_Changed(object sender, Control2ChangedEventArgs e)
            {
                this.Label1.Text = e.ChangedText;
            }

    But I've got an "Object not reference to instance of an object" error using FindControl method to find Control2 instance. How can I find it? May be there are other ways of interoperation between custom controls?

    Thanks in advance for your help,
    Vladislav.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    26 Apr 2011
    Link to this post
    Hi Vladislav,

    Use a query string value. When you click the button append a query string key in the url and read this value from the second control.

    Regards,
    Ivan Dimitrov
    the Telerik team

2 posts, 0 answered