Nov 12, 2012, 3:34 PM
Must selecting an item in a ComboBox really be so difficult?
Using 2.1 revision 4492
Windows 7 Professional SP1
IIS 7.5
Visual Studio 2010
Want one function with which I can select a item in a combox in all situations.
So during page load, directevents and with or without store or storeid.
After looking at not very helpfull basic examples and finding nothing exactly about this in the forum I start this thread.
Maybe filter for version 1 or 2 would be helpfull in the quicksearch?
This is what I came up with after lot of trying:
This is my testpage:
Windows 7 Professional SP1
IIS 7.5
Visual Studio 2010
Want one function with which I can select a item in a combox in all situations.
So during page load, directevents and with or without store or storeid.
After looking at not very helpfull basic examples and finding nothing exactly about this in the forum I start this thread.
Maybe filter for version 1 or 2 would be helpfull in the quicksearch?
This is what I came up with after lot of trying:
private void ChangeComboBoxValue(ComboBox cb, string value)
{
if (cb.Store.Count > 0 || !string.IsNullOrEmpty(cb.StoreID))
// Returns 'App.cbFive.select(3);App.cbSix.select(3);'
// Only works with Store and StoreID
ResourceManager.AddInstanceScript(string.Format("{0}.select({1});", cb.ClientID, value));
else
{
if (X.IsAjaxRequest)
// Returns 'App.cbOne.select(\"3\");App.cbTwo.select(\"3\");App.cbThree.select(\"3\");App.cbFour.select(\"3\");'
// Doesn't work for Store and StoreID
cb.Select(value);
else
// Returns 'selectedItems:[{value:"2"}]'
// Doesn't work for Store and StoreID
cb.SelectedItems.Add(new Ext.Net.ListItem { Value = value });
}
}
This is all I needed in version 1: private void ChangeComboBoxValue(ComboBox cb, string value)
{
cb.SelectedItem.Value = value;
}
Must be overlooking something.This is my testpage:
<%@ Page Language="C#" %>
<script runat="server" type="text/C#">
protected void Page_Load(object sender, EventArgs e)
{
if (X.IsAjaxRequest || IsPostBack) return;
var data = new []
{
new ComboItem(1, "Item1"),
new ComboItem(2, "Item2"),
new ComboItem(3, "Item3"),
new ComboItem(4, "Item4"),
};
dsOne.DataSource = data;
dsOne.DataBind();
foreach (var cItem in data)
{
cbOne.Items.Add(new Ext.Net.ListItem(cItem.Name, cItem.ComboItemId.ToString()));
cbTwo.Items.Add(new Ext.Net.ListItem { Text = cItem.Name, Value = cItem.ComboItemId.ToString() });
cbThree.Items.Add(new Ext.Net.ListItem { Text = cItem.Name, Value = cItem.ComboItemId.ToString() });
cbFour.Items.Add(new Ext.Net.ListItem { Text = cItem.Name, Value = cItem.ComboItemId.ToString() });
}
ChangeComboBoxValue(cbOne, "2");
ChangeComboBoxValue(cbTwo, "2");
ChangeComboBoxValue(cbThree, "2");
// Four set in Markup
ChangeComboBoxValue(cbFive, "2");
ChangeComboBoxValue(cbSix, "2");
}
protected void ChangeSelection(object sender, DirectEventArgs e)
{
ChangeComboBoxValue(cbOne, "3");
ChangeComboBoxValue(cbTwo, "3");
ChangeComboBoxValue(cbThree, "3");
ChangeComboBoxValue(cbFour, "3");
ChangeComboBoxValue(cbFive, "3");
ChangeComboBoxValue(cbSix, "3");
//ResourceManager.AddInstanceScript("var index = App.dsOne.find('ComboItemId', '3', false); alert('Index: ' + index); var recordId = App.dsOne.getAt(index).internalId; alert('RecordId: ' + recordId);");
}
private void ChangeComboBoxValue(ComboBox cb, string value)
{
if (cb.Store.Count > 0 || !string.IsNullOrEmpty(cb.StoreID))
// Returns 'App.cbFive.select(3);App.cbSix.select(3);'
// Only works with Store and StoreID
ResourceManager.AddInstanceScript(string.Format("{0}.select({1});", cb.ClientID, value));
else
{
if (X.IsAjaxRequest)
// Returns 'App.cbOne.select(\"3\");App.cbTwo.select(\"3\");App.cbThree.select(\"3\");App.cbFour.select(\"3\");'
// Doesn't work for Store and StoreID
cb.Select(value);
else
// Returns 'selectedItems:[{value:"2"}]'
// Doesn't work for Store and StoreID
cb.SelectedItems.Add(new Ext.Net.ListItem { Value = value });
}
//if (X.IsAjaxRequest)
//{
// if (cb.Store.Count == 0 && string.IsNullOrEmpty(cb.StoreID))
// cb.Select(value);
// else
// ResourceManager.AddInstanceScript(string.Format("{0}.select({1});", cb.ClientID, value));
// //cb.Select(value); // <-- Doesn't work for store
// //cb.SetValue(value); // <-- Doesn't work for store
// //cb.SetValueAndFireSelect(value); // <-- Doesn't work for store
// //cb.Select(2); // <-- Works for all but is index
// //cb.SelectedItems.Add(new Ext.Net.ListItem { Value = value }); //<-- Doesn't work
// //cb.SelectedItems.Add(new Ext.Net.ListItem { Index = 2 }); //<-- Doesn't work
//}
//else
//{
// if (cb.Store.Count == 0 && string.IsNullOrEmpty(cb.StoreID))
// cb.SelectedItems.Add(new Ext.Net.ListItem { Value = value });
// else
// ResourceManager.AddInstanceScript(string.Format("{0}.select({1});", cb.ClientID, value));
// //cb.SelectedItems.Add(new Ext.Net.ListItem { Value = value }); //<-- Doesn't work for store
// //cb.SelectedItems.Add(new Ext.Net.ListItem { Index = 1 }); //<-- Works for all also store but is index
// //cb.Select(value); // <-- Doesn't work for store
//}
}
public class ComboItem
{
public ComboItem(int id, string name) { ComboItemId = id; Name = name; }
public int ComboItemId { get; set; }
public string Name { get; set; }
}
</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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<ext:ResourceManager runat="server" />
<ext:Viewport runat="server" Layout="FormLayout">
<Items>
<ext:ComboBox runat="server" ID="cbOne" EmptyText="No selection" Editable="False" />
<ext:ComboBox runat="server" ID="cbTwo" EmptyText="No selection" Editable="False" />
<ext:ComboBox runat="server" ID="cbThree" EmptyText="No selection" Editable="False" />
<ext:ComboBox runat="server" ID="cbFour" EmptyText="No selection" Editable="False">
<SelectedItems>
<ext:ListItem Value="2" />
</SelectedItems>
</ext:ComboBox>
<ext:ComboBox runat="server" ID="cbFive" ValueField="ComboItemId" DisplayField="Name" EmptyText="No selection" Editable="False">
<Store>
<ext:Store runat="server" ID="dsOne">
<Model>
<ext:Model runat="server" IDProperty="ComboItemId">
<Fields>
<ext:ModelField Name="ComboItemId" Type="Int" />
<ext:ModelField Name="Name" />
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>
</ext:ComboBox>
<ext:ComboBox runat="server" ID="cbSix" StoreID="dsOne" ValueField="ComboItemId" DisplayField="Name" EmptyText="No selection" Editable="False" />
<ext:Button runat="server" Text="Change to Item3">
<DirectEvents>
<Click OnEvent="ChangeSelection" />
</DirectEvents>
</ext:Button>
</Items>
</ext:Viewport>
</form>
</body>
</html>