开发者

DevExpress ASPxComboBox SelectItem is null

I have an ASPxComboBox which I was able to get filtering correctly on user input. Now I want to save the selected item to the database. But when I try to get the SelectedItem it is 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="dis开发者_Go百科play_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");
    }

At the line:

int iTest = (int)cboInstructor.SelectedItem.GetValue("employee_id");

cboInstructor.SelectedItem is null. Anyone have an idea why?


Anthony,

The SelectedItem is used to specify the item to select. Once an item is selected in the ASPxComboBox, you can use the Value or Text property to reference it.

http://documentation.devexpress.com/#AspNet/DevExpressWebASPxEditorsASPxComboBoxMembersTopicAll

This thread may also help you: http://community.devexpress.com/forums/t/61424.aspx

Thanks.


I know this question is old, but in case someone is looking through it. Here are other possible solutions:

  1. Make sure the ValueType matches the actual 'Value' type as per your table. Some Value types are not populated automatically, like "System.Guid". But they are available. Essentially DevExpress will use any value type your table is using, even if it's not listed, if you type it manually, it will pick it up.

  2. By design, the ASPxComboBox does not synchronize to the server-side. To fix this, always bind your ASPxComboBox at every page request. So essentially your page load would look like this

    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();
    }
    

Ideally you would have a method that reloads your ASPxComboBox and you would just call that method instead of repeating this code everytime

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜