Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASPxComboBox - How to set selected item?

I'm using : ASPxComboBox

The problem is how to set selectedValue from code behind? If my html is like this:

<dxe:ASPxComboBox ID="cbxJobType" runat="server" width="200px" MaxLength="50">
    <Items>
        <dxe:ListEditItem Text="Contract" Value="0" />
        <dxe:ListEditItem Text="Full Time" Value="1" />
        <dxe:ListEditItem Text="Part Time" Value="2" />
    </Items>
    <ValidationSettings ErrorDisplayMode="ImageWithTooltip">
        <RequiredField ErrorText="Required Value" IsRequired="True" />
    </ValidationSettings>
</dxe:ASPxComboBox>
like image 670
Arti Patel Avatar asked Nov 29 '12 19:11

Arti Patel


3 Answers

You can either:

  • Set the ASPxComboBox.SelectedIndex property;

  • Select the required Item by its Value via the ASPxComboBox.Value property:

Code Behind:

cbxJobType.SelectedIndex = 0;
//or
cbxJobType.Value = "0";
like image 163
Mikhail Avatar answered Nov 01 '22 08:11

Mikhail


On the client side, I found there is the equivalent of Ruchi's suggestion:

cbxJobType.SelectedItem = cbxJobType.Items.FindByValue("Value #2");

Which is:

cbxJobType.SetSelectedItem(cbxJobType.FindItemByValue("Value #2"));
// or
cbxJobType.SetSelectedItem(cbxJobType.FindItemByText("Text #2"));

Go here to learn more about the ASPxComboBox on the client side (ASPxClientComboBox).

Go here to learn more about the ASPxComboBox on the server side.

There you can browse through all their members, constructors, events and methods.

like image 45
actaram Avatar answered Nov 01 '22 10:11

actaram


Client-Side Script

Give ClientInstanceName property to comboBoxto access it client side and ID property as cbxJobType to access control server side.

 // by text
    comboBox.SetText('Text #2');
    // by value
    comboBox.SetValue('Value #2');
    // by index
    comboBox.SetSelectedIndex(1); 

Server-Side Code

// by text
cbxJobType.Text = "Text #2";
// by value
cbxJobType.Value = "Value #2";
// by index
cbxJobType.SelectedIndex = 1; 

This code works fine too:

cbxJobType.SelectedItem = cbxJobType.Items.FindByValue("Value #2");
like image 26
Ruchi Avatar answered Nov 01 '22 09:11

Ruchi