Asp.net

ASPxComboBox - 如何設置所選項目?

  • January 28, 2019

我正在使用:ASPxComboBox

問題是如何從後面的程式碼中設置 selectedValue?如果我的html是這樣的:

<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>

客戶端腳本

將 ClientInstanceName 屬性賦予 ComboBox 以在客戶端訪問它,並將 ID 屬性作為 cbxJobType 以訪問控制伺服器端。

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

伺服器端程式碼

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

此程式碼也可以正常工作:

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

引用自:https://stackoverflow.com/questions/13633256