none
Issues with DropDownlist DataBinding RRS feed

  • Question

  • Hi there,

    I'm completely new to the Entity Framework world so please bear with me.

    I'm trying to create a new FieldTemplate for "States". The _Edit.ascx will comprise of a DropDownList that will list all US states.

    In my database, I have a table that stores all the US states and would like to bind my DropDownList to it as such:

    <asp:DropDownList ID="DropDownList1" DataSourceID = "States" DataTextField="State_Abbrev" DataValueField = "StateID" runat="server" CssClass="DDDropDown" AutoPostBack="True" >
    </asp:DropDownList>
    
    <asp:EntityDataSource ID="States" runat="server" 
        ConnectionString="name=MyDB" 
        DefaultContainerName="MyDB" 
        EnableFlattening="False" EntitySetName="lkp_states" 
        EntityTypeFilter="lkp_states">
    </asp:EntityDataSource>

    I would also like to add a Default item to my dropdown such as "Select a state", however when I add to my DropDown such as

    <asp:DropDownList ID="DropDownList1" DataSourceID = "States" DataTextField="State_Abbrev" DataValueField = "StateID" runat="server" CssClass="DDDropDown" AutoPostBack="True" >
    <asp:ListItem Text="Select a state" Value ="-1"></asp:ListItem>
    </asp:DropDownList>

    or from the code behind:

    DropDownList1.Items.Insert(0, new ListItem("Select state", "-1"));
    DropDownList1.SelectedIndex = 0;

    It doesn't add that default item in the final result.. Any ideas why?

    Thanks!

    FYI, this is the code behind for the _Edit.ascx file:

    public partial class DynamicData_FieldTemplates_States_EditField : System.Web.DynamicData.FieldTemplateUserControl {
    
    
        protected void Page_Load(object sender, EventArgs e) {
            SetUpValidator(RequiredFieldValidator1);
            SetUpValidator(RegularExpressionValidator1);
            SetUpValidator(DynamicValidator1);               
        }
        
        protected override void ExtractValues(IOrderedDictionary dictionary) {
            dictionary[Column.Name] = ConvertEditedValue(DropDownList1.Text);
        }
    
        public override Control DataControl {
            get {
                return DropDownList1;
            }
        }
    }

    Friday, May 10, 2013 3:59 PM

Answers

All replies