Search code examples
asp.netbindingcase-sensitiveselectedvalue

Asp net Bind() selectedvalue case sensitive


My RadioButtonList :

<asp:RadioButtonList ID="rblType" runat="server" selectedvalue='<%#Bind("Type")%>' AutoPostBack="true" >
         <asp:ListItem Text="X" Value="X" />
         <asp:ListItem Text="E" Value="E" />
         <asp:ListItem Text="H" Value="H" />
         <asp:ListItem Text="F" Value="F" />
         <asp:ListItem Text="A" Value="A" />
</asp:RadioButtonList>

It is inside a formview:

<asp:FormView ID="fv" runat="server" DataSourceID="ds" DataKeyNames="ChartId,Type,ItemId"
    OnModeChanged="fv_ModeChanged">

Datasource :

<asp:EntityDataSource ID="ds" runat="server" 
    ContextTypeName="Model.Entities"
    EntitySetName="ItemCharts" EnableInsert="true" EnableUpdate="true" AutoGenerateWhereClause="true"
    OnInserting="ds_Inserting" >
    <WhereParameters>
        <asp:QueryStringParameter Name="ItemId" DbType="Int32" QueryStringField="id" />
        <asp:QueryStringParameter Name="ChartId" DbType="Int32" QueryStringField="chart" />
        <asp:QueryStringParameter Name="Type" DbType="String" QueryStringField="type" />
    </WhereParameters>
</asp:EntityDataSource>

Error message :

has a SelectedValue which is invalid because it does not exist in the list of items

Cause :

The value I get from Bind() is a lower case 'x', the value of the ListItem is upper case 'X'.

I have lower case 'x' and upper case 'X' in my database.

I'm trying to find a way to keep the 'Bind' functionnalities, and select a listItem ignoring case sensitivity.

Any suggestions?

Thank you!


Solution

  • The Bind expression can be used with a custom format, but in .Net there is no way to instruct the format to "lowercase" or "uppercase" the object, so you need to build a custom format provider. I don't know how "inject" such a provider to the Bind expression or if even it's possible (or if it worths the effort).

    Since you are using EF, you can benefit on the partial methods used when a property is changed so you can write code to always save the Type in Uppercase.

      partial void OnTypeChanged()
            {
               if (this._Type != null)
                 this._Type = this._Type.ToUpper();
            }