PDA

View Full Version : [CLOSED] How to select existing item in ComboBox on the server?



vadym.f
Jul 16, 2013, 7:59 PM
Hi,

It's probably a very basic question. I need to select an existing list item on the server. The code below doesn't work. Could you please suggest the right approach or point out what's wrong?



<%@ 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)
{
var list = new List<object>
{
new {Text = "Text3", Value = 3},
new {Text = "Text4", Value = 4},
new {Text = "Text5", Value = 5}
};

this.Store1.DataSource = list;
this.Store1.DataBind();

// This call doesn't work
ComboBox1.SelectedItems.Add(new Ext.Net.ListItem { Value = "4" });

// Item reference resolves to null so this call doesn't work
/*
var item = ComboBox1.Items.Where(it => it.Value.Equals("4")).FirstOrDefault();
if (item != null)
{
ComboBox1.SelectedItems.Add(item);
}
*/

// This call doesn't work either
//ComboBox1.Select(1);
}
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Ext.Net 2.x</title>
<ext:ResourcePlaceHolder ID="ResourcePlaceHolder1" runat="server" Mode="Script" />
<ext:ResourcePlaceHolder ID="ResourcePlaceHolder2" runat="server" Mode="Style" />
</head>
<body>
<form id="Form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server">
</ext:ResourceManager>
<ext:Viewport runat="server" Layout="FitLayout">
<Items>
<ext:Panel runat="server">
<Items>
<ext:ComboBox ID="ComboBox1" runat="server" DisplayField="Text" ValueField="Value"
FieldLabel="Select" QueryMode="Local">
<Store>
<ext:Store ID="Store1" runat="server">
<Model>
<ext:Model ID="Model1" runat="server" IDProperty="Value">
<Fields>
<ext:ModelField Name="Text" />
<ext:ModelField Name="Value" />
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>
</ext:ComboBox>
</Items>
</ext:Panel>
</Items>
</ext:Viewport>
</form>
</body>
</html>

Baidaly
Jul 16, 2013, 9:45 PM
Hello!

Values in the list should have string values:



<%@ 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)
{
var list = new List<object>
{
new {Text = "Text3", Value = "3" },
new {Text = "Text4", Value = "4" },
new {Text = "Text5", Value = "5" }
};

this.Store1.DataSource = list;
this.Store1.DataBind();

ComboBox1.SelectedItems.Add(new Ext.Net.ListItem { Value = "4" });
}
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Ext.Net 2.x</title>
<ext:ResourcePlaceHolder ID="ResourcePlaceHolder1" runat="server" Mode="Script" />
<ext:ResourcePlaceHolder ID="ResourcePlaceHolder2" runat="server" Mode="Style" />
</head>
<body>
<form id="Form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server">
</ext:ResourceManager>
<ext:Viewport runat="server" Layout="FitLayout">
<Items>
<ext:Panel runat="server">
<Items>
<ext:ComboBox ID="ComboBox1" runat="server" DisplayField="Text" ValueField="Value"
FieldLabel="Select" QueryMode="Local">
<Store>
<ext:Store ID="Store1" runat="server">
<Model>
<ext:Model ID="Model1" runat="server" IDProperty="Value">
<Fields>
<ext:ModelField Name="Text" />
<ext:ModelField Name="Value" />
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>
</ext:ComboBox>
</Items>
</ext:Panel>
</Items>
</ext:Viewport>
</form>
</body>
</html>

vadym.f
Jul 16, 2013, 10:08 PM
Thanks much Daulet! Your suggestion works. I've got to convert the key Integer ID fields in the Models to String and then create lists from original data sources to databind the combo stores. It feels unfortunate that item selection on the server has to draw such an overhead going against natural ModelField data types.

Baidaly
Jul 17, 2013, 12:38 AM
You also can use numbers in this way:



protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
var list = new List<object>
{
new {Text = "Text3", Value = 3 },
new {Text = "Text4", Value = 4 },
new {Text = "Text5", Value = 5 }
};

this.Store1.DataSource = list;
this.Store1.DataBind();

ComboBox1.SelectedItems.Add(new Ext.Net.ListItem(4));
}
}

vadym.f
Jul 17, 2013, 4:07 AM
Thanks again! That was it, please close this thread down.