PDA

View Full Version : [CLOSED] GetStore() on ComboBox with static ListItems wipes out the ListItem collection



exe
Oct 30, 2015, 1:40 PM
GetStore() on ComboBox with static ListItems wipes out the ListItem collection. Try to remove this line:



Ext.Net.Store myStore = MyCombo.GetStore();


... and the sample will work with no exception.

What's happening here?

Thank you!

-r-



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

<%@ Import Namespace="System.Data" %>

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{

// this line will wipe out the ListItem collection
Ext.Net.Store myStore = MyCombo.GetStore();
//

string value = MyCombo.SelectedItem.Value;

if (string.IsNullOrEmpty(value))
{
Store store = MyCombo.GetStore();

if (store != null)
{
DataTable dt = null;

if (store.DataSource is DataTable)
{
dt = store.DataSource as DataTable;
}

if (store.DataSource is DataSet)
{
DataSet ds = store.DataSource as DataSet;
if (ds.Tables.Count > 0)
{
dt = ds.Tables[0];
}
}

if (dt != null && dt.Rows.Count > 0)
{
value = Convert.ToString(dt.Rows[0][MyCombo.ValueField]);
}
else
{
value = MyCombo.Items[0].Value;
}
}
}

// do something with value
}
</script>

<!DOCTYPE html>

<html>
<head id="Head1" runat="server">

</head>
<body>
<form id="Form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server" />

<ext:ComboBox
InputWidth="350"

ID="MyCombo"
runat="server"

FieldLabel="MyCombo"
PreselectFirstItem="True"
EmptyText=""
Editable="false"
QueryMode="Local"
TriggerAction="All"

AllowBlank="false">

<Items>
<ext:ListItem Text="AAA" Value="1" />
<ext:ListItem Text="BBB" Value="2" />
<ext:ListItem Text="CCC" Value="3" />
</Items>

</ext:ComboBox>

</form>
</body>
</html>

Daniil
Oct 30, 2015, 2:07 PM
Hi @exe,

A ComboBox's GetStore() generates a SimpleStore (not a public class) and it moves ComboBox's Items to that SimpleStore. I would say it should happen at the first .GetStore() call. I mean I would personally expect it throws an exception with only

Store store = MyCombo.GetStore();

Well, this exception unveils the fact that a ComboBox is not quite designed to use both - Store and Items. It supposes binding data through Store or Items, but not together.

Though, if you need it for some reason, please use this to access a ComboBox's Store.

Store store = MyCombo.Store.Primary;

exe
Oct 30, 2015, 2:15 PM
Hi Daniil,
I just need to know, if there is a Store bound to the Combo or it uses static ListItems. I am not using them both at a time.

I'm doing something like this:


if (MyCombo.GetStore().StoreID != null) {
// we have a Store
} else {
// we have ListItems
}


Or is there any other way to determine this?

Thank you!

Daniil
Oct 30, 2015, 2:43 PM
Yes, please use if (MyCombo.Store.Primary != null) { ....

exe
Oct 30, 2015, 2:54 PM
Cool, close it.
Thanks!