views:

234

answers:

1

Hello,

I am trying to create a custom property in a extended control, so that it can be acessed in both ASPX and C#. The problem wasn't actually creating 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 am sure that this is not the correct way of doing this... for many reasons :(

For example:

I can do: btnIcon.IconName = Icon. //and the names will appear here but they will not appear if I do:

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

And I am also using two properties instead of using only one... but I tried to combine them in to a single one... but i didn't sucess... so only IconName2 appears in the ASPX.

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

+1  A: 

If you want a property with a pre-defined, limited set of values to use in an ASPX control, you need to use an enum:

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

An enum can't be backed by string values though (unfortunately?) so you'll still have to have some other code which 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;
    //...
}
Rex M
Thx a lot man! That worked really nice.
M. Silva