Binding asp:DropDownList SelectedValue to a Boolean doesn't work - any workarounds?

StackOverflow https://stackoverflow.com/questions/20913704

  •  24-09-2022
  •  | 
  •  

Pergunta

Our UX team doesn't like the checkbox for handling a boolean - they want a asp:DropDownList with two options for true/false.

My bool has to be bound using <%# Bind("") %>, as it's in the edit template of a asp:GridView.

Here is my code:

<asp:GridView ...>
    ...
    <Columns>
        ...
        <asp:TemplateField ...>
            ...
            <EditItemTemplate>
                <asp:DropDownList ID="ExcludedDropDown" runat="server" SelectedValue='<%# Bind("IsExcluded") %>'>
                    <asp:ListItem Value="false" Text="Include" meta:resourcekey="ListItemResource0"></asp:ListItem>
                    <asp:ListItem Value="true" Text="Exclude" meta:resourcekey="ListItemResource1"></asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
        ...
    </Columns>
</asp:GridView>

With a breakpoint inside the EditItemTemplate, I tried following in the immediate window:

Eval("Exclude")
false

Where "false" would be the result of the Eval. Just for good measure, I did try to change the value of my "true" item to: "True", "T", "t", "Y", "y", "-1","0", "1", "" And all yield the same exception:

'DropDownList' has a selectedvalue which is invalid because it does not exist in the list of items.

I tried to work around using the "OnDataBinding" event, but this didn't help me at all (maybe I just did it wrong).

I prefer not to add a property to our Class that converts the bool into a string/integer (as that would work right away).

Is it impossible to bind a bool to DropDownList - and if it is, why would it make sense to have this restriction in ASP.NET? Have a hard time seeing the difference between supporting integers and boolean in DropDownList.

Foi útil?

Solução

I changed ListItem's Value start with capital (True|False), and it works fine. You might want to give a try.

enter image description here

<asp:DropDownList ID="ExcludedDropDown" runat="server" 
    SelectedValue='<%# Bind("IsExcluded") %>'>
    <asp:ListItem Value="True" Text="Include"></asp:ListItem>
    <asp:ListItem Value="False" Text="Exclude"></asp:ListItem>
</asp:DropDownList>

Here is how I test

public class Something
{
    public string Some { get; set; }
    public bool IsExcluded { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var collections = new List<Something>
        {
            new Something {Some = "One", IsExcluded = true},
            new Something {Some = "Two", IsExcluded = false},
        };
        GridView1.DataSource = collections;
        GridView1.DataBind();
    }
}

Outras dicas

Thank you for the answer! Knowing the "correct" approach narrowed my search to find the actual bug. By mistake, the meta:resourcekey was mapped (by our translation tool) to handle the Value. Removing the meta:resourcekey attribute and changing to capitalized True/False solved it for me. Here is the new code with the changes applied:

<asp:GridView ...>
    ...
    <Columns>
        ...
        <asp:TemplateField ...>
            ...
            <EditItemTemplate>
                <asp:DropDownList ID="ExcludedDropDown" runat="server" SelectedValue='<%# Bind("IsExcluded") %>'>
                    <asp:ListItem Value="False" Text="Include" ></asp:ListItem>
                    <asp:ListItem Value="True" Text="Exclude" ></asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
        ...
    </Columns>
</asp:GridView>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top