A custom control with a nullable property that has a default value

I can create a custom control with a default value:

private bool exclue = false;
public bool Exclude { get { return exclue; } set { exclue = value; } }

I can create the same thing with the nullable property:

private EntityStatuses? status = EntityStatuses.Active;
public EntityStatuses? Status { get { return status; } set { status = value; } }

But how can I set the null property in the markup when using a custom control?

<MyControls:Control ID="Con" runat="server" Status="?" >
+3
source share
3 answers

There is a workaround (with restriction) for the nullable property, which should be set to zero in the markup.

, <%= %> , - ( , ). :

<MyControls:Control ID="Con" runat="server" Status="<%#(EntityStatuses?)null %>">

: DataBind() , . - , DataBind().

, .

+1

null ?

private EntityStatuses? status = null;
public EntityStatuses? Status { get { return status; } set { status = value; } }

<MyControls:Control ID="Con" runat="server" >
0

Use two properties with different types:

<MyControls:Control runat="server" StatusString="Active" />

public string StatusString // string! because all values in markup are strings
{
    set
    {
        EntityStatuses s;
        if (Enum.TryParse(value, out s))
            this.status = s; // local variable
    }
}

public EntityStatuses Status
{
    get { return this.status; }
}

or use the built-in code block:

<MyControls:Control runat="server" Status='<%= EntityStatuses.Active %>' />
0
source

Source: https://habr.com/ru/post/1791178/


All Articles