Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
What i need to do to create custom control derived from textbox that set output error message of database verfication to output Label So,

i got exception Cannot create an object of type 'System.Web.UI.WebControls.Label' from its string representation 'Label1'

i try these both code :

C#
public Label LabelMain
       {
            get; set;
       }

        public Label c
        {
            get { return FindControl(c.ID) as Label; }
            set { c = value as Label; }
        }



first one shows that exception,
second one KILL THE COMPILER and visual studio EXPLODE !!! YAAH WHY !!!

What i should do ?
i extremely
C#

search google, stackoverflow, .... and nothing fix my problem;

What I have tried:

i try casting The Returned Label As Label, but Visual Studio Exploded.

CSC.exe exploded too
Posted
Updated 27-Feb-17 8:38am
v2
Comments
[no name] 27-Feb-17 9:24am    
Also I May Target Many Controls As Output Result From My Custom Control For Example,
Focusing To Next Control Developer Set
[no name] 27-Feb-17 12:55pm    
please i want this fix fast ??? guys ???

1 solution

Quote:
C#
public Label c
{
    ...
    set { c = value as Label; }
}

A classic example of infinite recursion. :)

You set c to something, which calls the setter for c, which calls the setter for c, which calls the setter for c, which calls...

Eventually, you'll get, not an "explosion", but a StackOverflowException. Which is almost always caused by infinite recursion.

You're going to need to split this into two properties: one of type Label, which can be set from the code-behind; and another of type string, which can be set to the label's ID from the markup:
C#
[TypeConverter(typeof(ControlIDConverter))]
[IDReferenceProperty]
public string LabelMainID
{
    get { return (string)ViewState["LabelMainID"] ?? string.Empty; }
    set { ViewState["LabelMainID"] = value; }
}

public Label LabelMain
{
    get
    {
        string id = LabelMainID;
        if (string.IsNullOrEmpty(id)) return null;
        return NamingContainer.FindControl(id) as Label;
    }
    set
    {
        if (value == null)
        {
            LabelMainID = null;
        }
        else
        {
            LabelMainID = value.ID;
        }
    }
}
 
Share this answer
 
v2
Comments
[no name] 28-Feb-17 2:01am    
#done i got it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900