PDA

View Full Version : [CLOSED] Problem to list through item combo from code behind



feanor91
Oct 11, 2012, 2:41 PM
Hello

Basicaly I want to do that:



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

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<!DOCTYPE html>

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

store.DataSource = new object[]
{
new object[] { ResourceManager.GetIconClassName(Icon.FlagFr), "France"},
new object[] { ResourceManager.GetIconClassName(Icon.FlagCa), "Canada"},
new object[] { ResourceManager.GetIconClassName(Icon.FlagDe), "Germany"},
new object[] { ResourceManager.GetIconClassName(Icon.FlagUs), "United States"},
new object[] { ResourceManager.GetIconClassName(Icon.FlagIt), "Italy"}
};

store.DataBind();

this.ResourceManager1.RegisterIcon(Icon.FlagFr);
this.ResourceManager1.RegisterIcon(Icon.FlagCa);
this.ResourceManager1.RegisterIcon(Icon.FlagDe);
this.ResourceManager1.RegisterIcon(Icon.FlagUs);
this.ResourceManager1.RegisterIcon(Icon.FlagIt);


for (int i = 0; i < this.ComboBox1.Items.Count-1; i++)
{
//do something
}

}
</script>

<html>
<head id="Head1" runat="server">
<title>IconCombo - Ext.NET Examples</title>
<link href="/resources/css/examples.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.icon-combo-item {
background-repeat : no-repeat !important;
background-position : 3px 50% !important;
padding-left : 24px !important;
}
</style>
</head>
<body>
<form id="Form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server" />

<h1>Render an Icon in ComboBox data items</h1>

<ext:ComboBox
ID="ComboBox1"
runat="server"
Width="250"
Editable="false"
DisplayField="name"
ValueField="name"
QueryMode="Local"
TriggerAction="All"
EmptyText="Select a country...">
<Store>
<ext:Store ID="Store1" runat="server">
<Model>
<ext:Model ID="Model1" runat="server">
<Fields>
<ext:ModelField Name="iconCls" />
<ext:ModelField Name="name" />
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>
<ListConfig>
<ItemTpl ID="ItemTpl1" runat="server">
<Html>
<div class="icon-combo-item {iconCls}">
{name}
</div>
</Html>
</ItemTpl>
</ListConfig>
<Listeners>
<Change Handler="if(this.valueModels.length>0){this.setIconCls(this.valueModels[0].get('iconCls'));}" />
</Listeners>
</ext:ComboBox>
</form>
</body>
</html>

(see last lines of the Page_Load function)

But items.count=0, so how to list all items in the combo?

I try to look at store level too, but found nothing until now.

Daniil
Oct 11, 2012, 6:38 PM
Hi @feanor91,

DataBind() doesn't populate the ComboBox Items collection. Generally, there can be any number of fields which you can bind to the Store, but ListItem can contain only two - Text and Value. So, I can't see anyway to support such behavior.

You should iterate the DataSource.

feanor91
Oct 11, 2012, 6:45 PM
OK,

But the databind is for this example, in my code I use a store to populate the combo (from an xmlp file, so I use an xml reader), is it the same behaviour?

Daniil
Oct 12, 2012, 5:51 AM
Do you load an XML file using AjaxProxy?

If so, then it is loaded via an AJAX request, so, the loaded data cannot be accessible on Page_Load.

You could send required data to server from a client when it is already loaded. Another approach is reading an XML file manually in Page_Load.

feanor91
Oct 12, 2012, 8:14 AM

Daniil
Oct 12, 2012, 11:33 AM
I have others combo in previous project that works like a charm. The only difference betwenn them and here is that here data came from an xml file and not a database.

Well, if you don't populate Items explicitly, they won't be presented.

How do you bind a data from a database?

feanor91
Oct 12, 2012, 11:44 AM
Like in the exemple I post upward, I call an aspx page taht return a JSON file

Daniil
Oct 12, 2012, 11:48 AM
Using AjaxProxy? And are you saying that the ComboBox Items collection is populated?

Do I understand correctly?

feanor91
Oct 12, 2012, 11:57 AM
Hum...I check, and no, you'r right, the combobox I think of, contains months, and I poupulated by hand, in fact, I never iterate through such a combox.

But, how can I get the datasource in that case?

Daniil
Oct 12, 2012, 12:02 PM
Well, the answer is here:



Do you load an XML file using AjaxProxy?

If so, then it is loaded via an AJAX request, so, the loaded data cannot be accessible on Page_Load.

You could send required data to server from a client when it is already loaded. Another approach is reading an XML file manually in Page_Load.

feanor91
Oct 12, 2012, 12:29 PM
OK, I will try that, thanks