[CLOSED] Combobox - DisplayField set in code behind

  1. #1

    [CLOSED] Combobox - DisplayField set in code behind

    Hi, how are you? I'm trying to set the DisplayField of a combobox in codebehind depending of another field in the same form. The comobobox works fine the first time the displayfield value is set, but it looks like the value of displayfield doesn't change from second time on.

    Here is the code:

    
    <ext:RadioGroup ID="rgrEmpresaParticular" runat="server" ColumnsNumber="2" AutomaticGrouping="false">                            
                                                <Items>
                                                    <ext:Radio ID="rbtEmpresa" runat="server" Name="EmpresaParticular" InputValue="1" BoxLabel="Empresa">
                                                    </ext:Radio> 
                                                    <ext:Radio ID="rbtParticular" runat="server" Name="EmpresaParticular" InputValue="2" BoxLabel="Particular">
                                                    </ext:Radio>
                                                </Items>
                                                <DirectEvents>
                                                    <Change OnEvent="EmpresaParticularModificado"></Change>
                                                </DirectEvents>
                                            </ext:RadioGroup>
    
    <ext:ComboBox 
                                                ID="ddlClientes" 
                                                runat="server" 
                                                ValueField="ID_CLIENTE"
                                                DisplayField="NOMBRE_CLIENTE"
                                                Editable="false"       
                                                NoteAlign="Top" Note="Clientes">
                                                <Store>
                                                    <ext:Store 
                                                        runat="server" 
                                                        ID="storeClientes" 
                                                        AutoLoad="false" >               
                                                        <Model>
                                                            <ext:Model ID="Model3" runat="server" IDProperty="ID_CLIENTE">
                                                                <Fields>
                                                                    <ext:ModelField Name="ID_CLIENTE" Type="Int" ServerMapping="ID_CLIENTE" />
                                                                    <ext:ModelField Name="CODIGO_AGENCIA" Type="String" ServerMapping="CODIGO_AGENCIA" />
                                                                    <ext:ModelField Name="NOMBRE_CLIENTE" Type="String" ServerMapping="NOMBRE_CLIENTE" />
                                                                </Fields>
                                                            </ext:Model>
                                                        </Model>
                                                    </ext:Store>
                                                </Store>   
                                            </ext:ComboBox>
    
    Public Sub EmpresaParticularModificado(ByVal sender As Object, ByVal e As Ext.Net.DirectEventArgs)
    
            Dim empresaParticular As Integer = Integer.Parse(rgrEmpresaParticular.CheckedItems(0).InputValue)
    
            RellenarComboClientes(empresaParticular)
    
        End Sub
    
    Private Sub RellenarComboClientes(ByVal empresaParticular As Integer)
    
            ddlClientes.Items.Clear()
            ddlClientes.Text = ""
    
            Dim InterfazClientes As New Negocio.InterfazClientes()
    
            Try
                Dim tablaClientes As Object = InterfazClientes.RecuperarClientes(empresaParticular)
    
                Select Case empresaParticular
                    Case 1
                        ddlClientes.DisplayField = "CODIGO_AGENCIA"
                        storeClientes.DataSource = tablaClientes
                        storeClientes.DataBind()
                    Case Else
                        ddlClientes.DisplayField = "NOMBRE_CLIENTE"
                        storeClientes.DataSource = tablaClientes
                        storeClientes.DataBind()
                End Select
    
            Catch ex As Exception
                MostrarPanelMensaje("alert alert-error", "No se ha podido leer la base de datos de clientes. Error: " & ex.Message)
            End Try
    
        End Sub
    Am I missing something?

    Thank you in advance
    Last edited by Daniil; Sep 11, 2013 at 2:03 PM. Reason: [CLOSED]
  2. #2
    Hi @jamesand,

    Yes, if a ComboBox's DisplayField is changed when its dropdown list is already opened once, then it doesn't take affect.

    I can suggest the following solution.

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                Store store = this.ComboBox1.GetStore();
                store.DataSource = new object[] 
                { 
                    new object[] { "1", "Initial Item 1", "Dynamic Item 1" },
                    new object[] { "2", "Initial Item 2", "Dynamic Item 2" },
                    new object[] { "3", "Initial Item 3", "Dynamic Item 3" }
                };
            }
        }
    
        protected void ChangeDisplayField(object sender, DirectEventArgs e)
        {
            X.Js.Call("changeDisplayField", JRawValue.From(this.ComboBox1.ClientID), "text2");
        }
    </script>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <title>Ext.NET v2 Example</title>
    
        <script>
            var changeDisplayField = function (combo, displayField) {
                if (combo.picker) {
                    combo.picker.destroy();
                    delete combo.picker
                }
    
                combo.displayField = displayField;
            };
        </script>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
    
            <ext:Button runat="server" Text="Change DisplayField" OnDirectClick="ChangeDisplayField" />
    
            <ext:ComboBox 
                ID="ComboBox1" 
                runat="server" 
                DisplayField="text1" 
                ValueField="value">
                <Store>
                    <ext:Store runat="server">
                        <Model>
                            <ext:Model runat="server">
                                <Fields>
                                    <ext:ModelField Name="value" />
                                    <ext:ModelField Name="text1" />
                                    <ext:ModelField Name="text2" />
                                </Fields>
                            </ext:Model>
                        </Model>
                        <Reader>
                            <ext:ArrayReader />
                        </Reader>
                    </ext:Store>
                </Store>
            </ext:ComboBox>        
        </form>
    </body>
    </html>
  3. #3
    Thanks Daniil, it works fine. but what if I want to display in the textbox "text2" once I click "ChangeDisplayField"? In your example, the combo displays the "text2" field, but when selected the extbox shows "text1".
  4. #4
    Yes, it needs to change a ComboBox's displayTpl as well.
    var changeDisplayField = function (combo, displayField) {
        if (combo.picker) {
            combo.picker.destroy();
            delete combo.picker
        }
     
        combo.displayField = displayField;
        combo.displayTpl.destroy();
        combo.displayTpl = new Ext.XTemplate(
            '<tpl for=".">' +
                '{[typeof values === "string" ? values : values["' + combo.displayField + '"]]}' +
                '<tpl if="xindex < xcount">' + combo.delimiter + '</tpl>' +
            '</tpl>'
        );
    };
  5. #5
    Ok works perfectly! Thank you

Similar Threads

  1. Replies: 7
    Last Post: Feb 27, 2013, 12:28 PM
  2. [CLOSED] ComboBox DisplayField with HTML?
    By peter.campbell in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Sep 24, 2012, 8:31 PM
  3. Get value of DisplayField of Combobox
    By littletran in forum 1.x Help
    Replies: 0
    Last Post: May 17, 2012, 7:32 AM
  4. Combobox DisplayField 2 fields???
    By 78fede78 in forum 1.x Help
    Replies: 3
    Last Post: Sep 06, 2010, 2:07 PM
  5. DisplayField of the ComboBox
    By Maia in forum 1.x Help
    Replies: 2
    Last Post: Jun 04, 2009, 10:46 AM

Tags for this Thread

Posting Permissions