PDA

View Full Version : [CLOSED] Assigning ComboBox Value



Adrian
Mar 10, 2012, 8:04 AM
Hi
ComboBox1.Value = "some value" works fine when values are hard-coded. When derived from a DataSource assigning the value appears to fail. Please see the following basic Northwind sample.



protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
ComboBox1.Value = "5";
}
}




<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="
SELECT
[EmployeeID],
[LastName]
FROM [Employees]"
/>


<ext:Store ID="Store1" runat="server" DataSourceID="SqlDataSource1">
<Model>
<ext:Model ID="Model1" runat="server" IDProperty="EmployeeID">
<Fields>
<ext:ModelField Name="EmployeeID" />
<ext:ModelField Name="LastName" />
</Fields>
</ext:Model>
</Model>
</ext:Store>


<ext:ComboBox
ID="ComboBox1"
runat="server"
StoreID="Store1"
DisplayField="LastName"
ValueField="EmployeeID">
</ext:ComboBox>

Daniil
Mar 10, 2012, 2:16 PM
Hi,

You should apply an integer value, not a string one.

ComboBox1.Value = 5;
because EmployeeID has integer type.

Adrian
Mar 10, 2012, 8:13 PM
Haha - that's what you get when looking at code when you're too tired. Thanks Daniil (you can close),
A.

Adrian
Mar 12, 2012, 2:39 AM
Actually there is a problem here ... using the example below if you click the button without making a selection from the combobox you will get an incorrect value (retrieving 'text' rather than 'value'). If you make a selection on the combobox and then click the button it works fine.



protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
ComboBox1.Value = 6;
}
}

protected void Button_Click(object sender, DirectEventArgs e)
{
X.Msg.Alert("ComboBox1 Value", "Combo Value: " + ComboBox1.SelectedItem.Value).Show();
}





<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="
SELECT
[EmployeeID],
[LastName]
FROM [Employees]"
/>


<ext:Store ID="Store1" runat="server" DataSourceID="SqlDataSource1">
<Model>
<ext:Model ID="Model1" runat="server">
<Fields>
<ext:ModelField Name="EmployeeID" />
<ext:ModelField Name="LastName" />
</Fields>
</ext:Model>
</Model>
</ext:Store>


<ext:ComboBox
ID="ComboBox1"
runat="server"
StoreID="Store1"
DisplayField="LastName"
ValueField="EmployeeID">
</ext:ComboBox>


<ext:Button ID="Button1" runat="server" Text="Click Me">
<DirectEvents>
<Click OnEvent="Button_Click" />
</DirectEvents>
</ext:Button>

Daniil
Mar 12, 2012, 12:01 PM
Confirmed.

Please populate the ComboBox SelectedItems collection instead of setting up its Value.

Example

protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
ComboBox1.SelectedItems.Add(new Ext.Net.ListItem()
{
CustomConfig =
{
new ConfigItem("value", "6", ParameterMode.Raw)
}
});
}
}