PDA

View Full Version : [CLOSED] Can not select combobox items using store



blurken
May 18, 2012, 12:42 AM
Hi,

I have changed from using ListItems in a combobox to using a store. When I use a store there is nothing that I can do to make the selected item shown as selected.

There are two methods that I can take when using list items.

1) Call a javascript function after render to select the value

myCombo.Listeners.AfterRender.Handler = "#{myCombo}.setValue('" & CStr(value) & "');"

2) Add a listIem to the SelectedItems Collection to select the value.

Dim selectedItem As New ListItem()
selectedItem.Value = CStr(value)
myCombo.SelectedItems.Add(selectedItem)

Neither of these methods work when I change to using a store, I can not understand why.

Can someone please tell me how to make a combobox show the selected value?

geoffrey.mcgill
May 18, 2012, 4:38 AM
Hi,

I'm not too sure what's going wrong in your situation, but the following sample does appear to function correctly.

Example


<%@ Page Language="C#" %>

<%@ Register assembly="Ext.Net" namespace="Ext.Net" tagprefix="ext" %>


<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
var store = this.ComboBox1.GetStore();

store.DataSource = new object[]
{
new object[] { "Australia" },
new object[] { "Austria" },
new object[] { "Canada" },
new object[] { "Russia" },
new object[] { "United Kingdom" }
};


store.DataBind();


this.ComboBox1.SelectedItems.Add(new Ext.Net.ListItem("Canada"));
}
</script>


<!DOCTYPE html>

<html>
<head runat="server">
<title>Ext.NET Example</title>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />


<ext:ComboBox
ID="ComboBox1"
runat="server"
TypeAhead="true"
DisplayField="name"
QueryMode="Local">
<Store>
<ext:Store runat="server">
<Model>
<ext:Model runat="server">
<Fields>
<ext:ModelField Name="name" />
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>
</ext:ComboBox>
</form>
</body>
</html>

If this doesn't help solve the issue, then I think you're going to have to post a more complete code sample demonstrating the full scenario.

blurken
May 18, 2012, 6:04 AM
<%@ Page Language="C#" %>

<%@ Register assembly="Ext.Net" namespace="Ext.Net" tagprefix="ext" %>


<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
var store = this.ComboBox1.GetStore();

store.DataSource = new object[]
{
new object[] {1, "Australia" },
new object[] {2, "Austria" },
new object[] {3, "Canada" },
new object[] {4, "Russia" },
new object[] {5, "United Kingdom" }
};


store.DataBind();


this.ComboBox1.SelectedItems.Add(new Ext.Net.ListItem("3"));
}
</script>


<!DOCTYPE html>

<html>
<head id="Head1" runat="server">
<title>Ext.NET Example</title>
</head>
<body>
<form id="Form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server" />


<ext:ComboBox
ID="ComboBox1"
runat="server"
TypeAhead="true"
DisplayField="name"
ValueField="id"
QueryMode="Local">
<Store>
<ext:Store ID="Store1" runat="server">
<Model>
<ext:Model ID="Model1" runat="server">
<Fields>
<ext:ModelField Name="id" />
<ext:ModelField Name="name" />
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>
</ext:ComboBox>
</form>
</body>
</html>

I worked out the problem. When I used a name/value pair the value was an integer and the selected item was a string, hence the above code wont work.

When I change the data-source to the following it works.


store.DataSource = new object[]
{
new object[] {"1", "Australia" },
new object[] {"2", "Austria" },
new object[] {"3", "Canada" },
new object[] {"4", "Russia" },
new object[] {"5", "United Kingdom" }
};

Lesson learned. All values in Stores used in a combobox must be strings to be selectable.

Thankyou for your help...

Daniil
May 18, 2012, 7:33 AM
Hi,

You can use integer as values. In this case you have to set up Mode to Raw for ListItem.

this.ComboBox1.SelectedItems.Add(new Ext.Net.ListItem()
{
Value = "3",
Mode = ParameterMode.Raw
});

There is Value Mode by default and it causes the Value to rendered as a string.

Setting up Mode to Raw causes the Value to be rendered as it is, i.e. as an integer.

blurken
May 21, 2012, 1:20 AM
Wow, brilliant, that has saved me a lot of grief. Thank you very much for your help...