我有一个ASPxComboBox,我可以在用户输入上正确过滤。现在我想将所选项目保存到数据库中。但是当我尝试获取SelectedItem时,它是null。
ASP
<dxe:ASPxComboBox ID="cboInstructor" runat="server" Width="100%"
EnableCallbackMode="True" CallbackPageSize="10"
IncrementalFilteringMode="Contains" ValueType="System.Int32" ValueField="employee_id"
OnItemsRequestedByFilterCondition="cboInstructor_OnItemsRequestedByFilterCondition_SQL"
OnItemRequestedByValue="cboInstructor_OnItemRequestedByValue_SQL" TextFormatString="{0} {1}"
DropDownStyle="DropDown"
>
<Columns>
<dxe:ListBoxColumn FieldName="display_forename" Caption="Forename" />
<dxe:ListBoxColumn FieldName="display_surname" Caption="Surname" />
</Columns>
</dxe:ASPxComboBox>
<asp:SqlDataSource ID="SqlDataSourceInstruct" runat="server" ConnectionString="Server=testserver;User ID=root;Password=password;Persist Security Info=True;Database=central" ProviderName="MySql.Data.MySqlClient" SelectCommand="GetUser" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter Name="filter" Type="String" />
<asp:Parameter Name="startIndex" Type="Int32" />
<asp:Parameter Name="endIndex" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSourceInstructPopulate" runat="server" ConnectionString="Server=testserver;User ID=root;Password=password;Persist Security Info=True;Database=central" ProviderName="MySql.Data.MySqlClient" SelectCommand="GetUser" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
<asp:Button ID="btnTest" runat="server" Text="Test" OnClick="btnTest_Click" />
CS
protected void Page_Load(object sender, EventArgs e)
{
}
protected void cboInstructor_OnItemsRequestedByFilterCondition_SQL(object source, ListEditItemsRequestedByFilterConditionEventArgs e)
{
ASPxComboBox comboBox = (ASPxComboBox)source;
SqlDataSourceInstruct.SelectParameters.Clear();
SqlDataSourceInstruct.SelectParameters.Add("filter", TypeCode.String, string.Format("%{0}%", e.Filter));
SqlDataSourceInstruct.SelectParameters.Add("startIndex", TypeCode.Int32, (e.BeginIndex + 1).ToString());
SqlDataSourceInstruct.SelectParameters.Add("endIndex", TypeCode.Int32, (e.EndIndex + 1).ToString());
comboBox.DataSource = SqlDataSourceInstruct;
comboBox.DataBind();
}
protected void cboInstructor_OnItemRequestedByValue_SQL(object source, ListEditItemRequestedByValueEventArgs e)
{
long value = 0;
if (e.Value == null)
return;
if (!Int64.TryParse(e.Value.ToString(), out value))
return;
ASPxComboBox comboBox = (ASPxComboBox)source;
SqlDataSourceInstructPopulate.SelectCommand = @"SELECT employee_id, display_surname, display_forename FROM user_record WHERE employee_id = @ID ORDER BY display_forename";
SqlDataSourceInstructPopulate.SelectCommandType = System.Web.UI.WebControls.SqlDataSourceCommandType.Text;
SqlDataSourceInstructPopulate.SelectParameters.Clear();
SqlDataSourceInstructPopulate.SelectParameters.Add("ID", TypeCode.Int64, e.Value.ToString());
comboBox.DataSource = SqlDataSourceInstructPopulate;
comboBox.DataBind();
comboBox.ValueField = "employee_id";
}
protected void btnTest_Click(object sender, EventArgs e)
{
int iTest = (int)cboInstructor.SelectedItem.GetValue("employee_id");
}
在这一行:
int iTest = (int)cboInstructor.SelectedItem.GetValue("employee_id");
cboInstructor.SelectedItem
为空。任何人都知道为什么?
答案 0 :(得分:1)
安东尼,
SelectedItem用于指定要选择的项目。在ASPxComboBox中选择一个项目后,您可以使用Value或Text属性来引用它。
http://documentation.devexpress.com/#AspNet/DevExpressWebASPxEditorsASPxComboBoxMembersTopicAll
此主题也可以帮助您: http://community.devexpress.com/forums/t/61424.aspx
感谢。
答案 1 :(得分:0)
我知道这个问题已经过时了,但万一有人正在审视它。以下是其他可能的解决方案:
确保ValueType与实际的&#39;值&#39;匹配。按照你的表格输入。某些值类型不会自动填充,例如&#34; System.Guid&#34;。但他们是可用的。基本上DevExpress将使用您的表正在使用的任何值类型,即使它没有列出,如果您手动键入它,它将接收它。
按照设计,ASPxComboBox不会与服务器端同步。要解决此问题,请始终在每个页面请求时绑定ASPxComboBox。所以基本上你的页面加载看起来像这样
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSourceInstruct.SelectParameters.Clear();
SqlDataSourceInstruct.SelectParameters.Add("filter",TypeCode.String, string.Format("%{0}%", e.Filter));
SqlDataSourceInstruct.SelectParameters.Add("startIndex", TypeCode.Int32, (e.BeginIndex + 1).ToString());
SqlDataSourceInstruct.SelectParameters.Add("endIndex", TypeCode.Int32, (e.EndIndex + 1).ToString());
cboInstructor.DataSource = SqlDataSourceInstruct;
cboInstructor.DataBind();
}
理想情况下,您将有一个重新加载ASPxComboBox的方法,而您只需调用该方法,而不是每次都重复此代码