Binding ComboBox

  1. #1

    Binding ComboBox



    Hi,

    I am filling a ComboBox as follows:

    <script type="text/javascript">
        var dataLoaded = function (store, records) {
            if (records.length > 0) 
                DetailsForm.form.loadRecord(records[0]);
            else 
                DetailsForm.form.reset();
        }
    </script>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Store1.DataBind();
            this.Store2.DataSource = new List<object>
            {
                new {Id = 0, Name = "Name 0"},
                new {Id = 1, Name = "Name 1"},
                new {Id = 2, Name = "Name 2"},
            };
            this.Store2.DataBind();
        }
    </script>
    
    <ext:Store ID="Store1" runat="server" DataSource="<%# Model.ToList() %>">                                 
        <Reader>
            <ext:JsonReader IDProperty="KEY" MessageProperty="message">
                <Fields>
                    <ext:RecordField Name="ID" SortDir="ASC"/>
                    <ext:RecordField Name="NAME" />
                    <ext:RecordField Name="DESC" />
                    <ext:RecordField Name="TYPE" Type="int" />
                </Fields>
            </ext:JsonReader>
        </Reader>
        <SortInfo Field="ID" Direction="ASC" /> 
        <Listeners>
            <Load Fn="dataLoaded" />
        </Listeners>
    </ext:Store>
    <ext:Store ID="Store2" runat="server">
        <Reader>
            <ext:JsonReader>
                <Fields>
                    <ext:RecordField Name="Id" />
                    <ext:RecordField Name="Name" />
                </Fields>
            </ext:JsonReader>
        </Reader>            
    </ext:Store>
    
    <ext:FormPanel ID="DetailsForm" runat="server" Url="/Test/Save/" Border="false">
        <Items>
            <ext:ComboBox
                ID="TYPE"
                runat="server" 
                StoreID="Store2"
                Editable="false"  
                DisplayField="Name"
                ValueField="Id"
                TypeAhead="true" 
                FieldLabel="Type"                                                                                    
                ForceSelection="true" 
                ItemCls="required"
                Width="100">
            </ext:ComboBox>
        </Items>
    </ext:FormPanel>
    ComboBox is loading data without any problem, but although the correct value is selected, the combo displays the integer TYPE as text.

    How can I select and display the correct value field?

  2. #2

    RE: Binding ComboBox

    Hi again,

    With the code of my previous post, If I declare ComboBox items explicitly then all works without problems:

    <ext:ComboBox
        ID="TYPE"
        runat="server" 
        Editable="false"  
        SelectedIndex="0"
        FieldLabel="Type"                                                                                    
        ForceSelection="true" 
        ItemCls="required"
        Width="100">
        <Items>
            <ext:ListItem Text="Name 0" Value="0" />
            <ext:ListItem Text="Name 1" Value="1" />
            <ext:ListItem Text="Name 2" Value="2" />
        </Items>
    </ext:ComboBox>
    How can I bind data dynamically to a ComboBox and get selecte the right item?
    Last edited by Dominik; Oct 26, 2010 at 3:23 PM.
  3. #3

    RE: Binding ComboBox



    Please,

    Does anyone knows how to dinamically load data and display selected item in a ConboBox?

  4. #4

    RE: Binding ComboBox

    Hi,
    I don't know if you've found the solution or not, but here is what I use:

    
    <%-- aspx page --%>
    
    
    
    <ext:Store ID="storeComboBox1" runat="server">
        <Reader>
            <ext:JsonReader>
                <Fields>
                    <ext:RecordField Name="col1" />
                </Fields>
            </ext:JsonReader>
        </Reader>
        <Listeners>
            <%--default value when refreshed--%>
            <Load Handler="#{cboComboBox1}.setValue(#{cboComboBox1}.store.getAt(0).get('col1'));" />
        </Listeners>
    </ext:Store>
    
    
    
    
    <ext:ComboBox ID="cboComboBox1" runat="server" StoreID="storeComboBox1">
    </ext:ComboBox>
    
    
    page_load (vb)
    
    
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        Dim conn As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))
        Dim adp1 As New SqlDataAdapter("select distinct col1 from dbo.Table1 where col1 >'' order by col1 desc", conn)
        Dim dt As New DataTable
            adp1.Fill(dt)
            'bind store
    
            storeComboBox1.DataSource = dt
            storeComboBox1.DataBind()
    
            cboComboBox1.StoreID = storeComboBox1.ID.ToString
            cboComboBox1.ValueField = "col1"
            cboComboBox1.DisplayField = "col1"
            cboComboBox1.DataBind()
    
    End Sub
  5. #5

    RE: Binding ComboBox

    Hi Steve,

    Thank you very much for address me to the right direction. What I really need in my case is this:


    <Listeners>
       <Load Handler="#{TYPE}.setValue(#{TYPE}.store.getAt(#{TYPE}.getSelectedIndex()).get('NAME'));" />
    </Listeners>



Similar Threads

  1. ComboBox data binding
    By AlexMaslakov in forum 1.x Help
    Replies: 3
    Last Post: Jun 13, 2013, 9:04 AM
  2. Data not binding into ComboBox
    By nagesh in forum 1.x Help
    Replies: 5
    Last Post: Jul 12, 2012, 2:35 PM
  3. Selecting value from ComboBox after binding
    By alexp in forum 1.x Help
    Replies: 0
    Last Post: Mar 30, 2011, 9:48 AM
  4. Two binding combobox not work by items way
    By animalisme in forum 1.x Help
    Replies: 1
    Last Post: Sep 28, 2009, 3:30 AM
  5. Combobox not binding data
    By Satyanarayana murthy in forum Open Discussions
    Replies: 0
    Last Post: Jun 27, 2009, 12:22 PM

Posting Permissions