Creating Property in ASP.Net Control for C # and ASPX

I am trying to create a custom property in an extended control so that it can be used in both ASPX and C #. The problem didn't actually create it ... because I managed to do it by applying the following code:

public static class Icon
{
    public static string activityMonitor = "activityMonitor.png";
    public static string addBlankPage = "addBlankPage.png";
    public static string addComment = "addComment.png";
    public static string addPageTable = "addPageTable.png";
}


public class myButtonIcon : LinkButton
{
    public myButtonIcon()
    {
    }

    [Bindable(false)]
    [Category("Properties")]
    [DefaultValue("")]
    [Localizable(true)]

    public string IconName { get; set; }
}

      

However, I'm pretty sure this is not the correct way to do it ... for many reasons :(

For example:

I can do: btnIcon.IconName = Icon. // and the names will appear here but they won't appear if I do this:

<myControl:myButtonIcon ID="btnTest" runat="server" IconName=" //names do not appear here></myControl:myButtonIcon>

      

And I also use two properties instead of using just one ... but I tried to combine them into one ... but I failed ... so only IconName2 appears in ASPX.

So ... I would really appreciate if you could explain this to me better. The code you see here was the result of a lot of research on the internet and many attempts. :(

+2


source to share


1 answer


If you need a property with a predefined, limited set of values ​​for use in an ASPX control, you need to use an enumeration:

public enum Icon
{
    ActivityMonitor,
    AddBlankPage,
    //...
}

      



Enumeration cannot be supported by string values, though (unfortunately?), So you still have to have some other code that determines the true filename to use based on the selected enum value:

public Icon Icon { get;set; }

//in some method
switch(this.Icon)
{
    case Icon.ActivityMonitor:
        this.IconUrl = "activityMonitor.png";
        break;
    case AddBlankPage:
        this.IconUrl = "addBlankPage.png";
        break;
    //...
}

      

+2


source







All Articles