如何检索连接到SQL数据源的DevExpress‘ASPxCombobox上显示的第一个值?页面首次加载后,如果我使用selectedItem检索ASPxCombo框上显示的第一个值,而不单击组合框,则selectedItem将得到"null“。当我单击并从组合框中选择一个值时,我可以使用selectedItem来获取该值。
发布于 2015-04-24 15:38:50
要访问ASPxComboBox下拉窗口中的元素,请使用其Items集合:
//retrieve value of the first element in dropdown window of ASPxComboBox
var firstItem = comboBox.Items[0].Value
发布于 2016-03-25 01:11:44
页面加载时,Combobox selecteditem将不可用。此时数据未绑定到控件,而是使用DataBound事件。
Webform1.aspx
<dx:ASPxComboBox ID="ASPxComboBox1" runat="server" DataSourceID="SqlDataSource2" TextField="CategoryName" ValueField="CategoryID">
</dx:ASPxComboBox>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:NWindConnectionString %>"
SelectCommand="SELECT * FROM [Categories]">
</asp:SqlDataSource>Webform1.aspx.cs
void ASPxComboBox1_DataBound(object sender, EventArgs e)
{
ASPxComboBox1.SelectedIndex = 0;
object selectedItem = ASPxComboBox1.SelectedItem;
string selectedValue = ASPxComboBox1.SelectedItem.Value.ToString();
}发布于 2016-04-28 04:04:28
在您的Page_Load中写入以下内容:
if (!IsPostBack)
{
cmb.DataBind();
cmb.SelectedIndex = 0;
}https://stackoverflow.com/questions/20612748
复制相似问题