PDA

View Full Version : [CLOSED] Combobox - DisplayField set in code behind



jamesand
Sep 10, 2013, 1:29 PM
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(empresaParticul ar)

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

Daniil
Sep 11, 2013, 4:58 AM
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>

jamesand
Sep 11, 2013, 10:32 AM
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".

Daniil
Sep 11, 2013, 12:37 PM
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>'
);
};

jamesand
Sep 11, 2013, 1:19 PM
Ok works perfectly! Thank you