How to pass a Control (Label) to your Custom Control

Joined
Aug 13, 2008
Messages
2
Reaction score
0
Hello,

I'm busy developing a custom control that extends a LinkButton. When someone moves the mouse over the link more information should appear in a Label. It is possible to make for every link a new Label in the control itself, but with 100 links that would mean that I also make 100 labels which increases that size of the HTML-code unneccesary. Besides, only one label can be shown 1 at a time.

I want it like this:

Code:
<asp:Label runat="server" ID="lbl" style="display:none;ponOsition:absolute;background-color:rgb(255,255,0)"></asp:Label>
<mc:LinkButtonWithAltDiv runat="server" ID="lb1" AltLabel="lbl" lblText="more info of link 1" Text="Link 1" CommandName="Select" />
<mc:LinkButtonWithAltDiv runat="server" ID="lb2" AltLabel="lbl" lblText="more info of link 2" Text="Link 2" CommandName="Select" />
<mc:LinkButtonWithAltDiv runat="server" ID="lb3" AltLabel="lbl" lblText="more info of link 3" Text="Link 3" CommandName="Select" />

Now the problem is in the part: AltLabel="lbl".

When the property is defined as:
Code:
private Label _label;
        public Label AltLabel
        {
            get { return _label; }
            set { _label = value; }
        }

I get the error "Cannot create an object of type 'System.Web.UI.WebControls.Label' from its string representation 'lbl' for the 'AltLabel' property.". This makes sense cause I pass a string instead of a Label.
I also tried the following but that didn't help either:
Code:
        private Label _label;
        public string AltLabel
        {
            get { return _label.ToString(); }
            set { _label = (Label)FindControl(value); }
        }

I really hope someone can help me.

Thanks in advance.

Kind regards,
Niels
 
Joined
Aug 13, 2008
Messages
2
Reaction score
0
Hello again,

I solved my problem. For people who're interested in the solution, I pass in the aspx-file only the ID of the label. In the overriden OnInit method I search the object with the passed ID.

Code:
        private Label _altLabel;

        private string _altLabelID;
        public string AltLabelID
        {
            get { return _altLabelID; }
            set { _altLabelID = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            if (AltLabelID != null)
            {
                _altLabel = (Label)FindControl(AltLabelID);
            }
            base.OnInit(e);
        }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top