开发者

Setting AspxComboBox.SelectedIndex not working after using LinqDataSource.Selecting. Any ideas?

I've posted this on the DevExpress forums as well, but you can't beat stackoverflow for good answers.

I seem to be having a problem with the DevExpress AspxComboBox control. I'm using DevExpress 9.1.11 controls with Visual Studio 2008.

Here are my controls:

<asp:LinqDataSource ID="ContactsDataSource" runat="server"
    ContextTypeName="DAL.MorrisDataContext"
    Select="new (Id, FullName)"
    TableName="Contact">
</asp:LinqDataSource>
<dxe:ASPxComboBox ID="SignerComboBox" runat="server" ToolTip="Select a Contact to use."
    AutoPostBack="True" DataSourceID="ContactsDataSource" TextField="FullName"
    ValueField="Id" ValueType="System.String" Width="140px" SelectedIndex="0">
</dxe:ASPxComboBox>

I am handling the Selecting event of the LinqDataSource to filter the list of items used in the ComboBox as follows:

Protected Sub ContactsDataSource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles ContactsDataSource.Selecting
    Dim Contacts As IList(Of Contact) = iEntity.Contacts.ToList
    e.Result = Contacts
End Sub

iEntity.Contacts is just a LINQtoSQL object containing a child collection of LINQtoSQL objects known as "Contacts". All of this works perfectly.

The problem comes when I try to add a new Contact to the collection of contacts for the Entity and then try to update the ComboBox to reflect the addition as follows:

Private Sub SignerUpdate()
    Dim m_Last = SignerContactLastTextBox.Text
    Dim m_First = SignerContactFirstTextBox.Text
    If m_Last <> "" OrElse m_First <> "" Then
        Dim m_Middle = SignerContactMiddleTextBox.Text
        Dim m_Suffix = SignerContactSuffixTextBox.Text
        Dim m_ContactIndex As Int32 = SignerComboBox.SelectedIndex
        Dim m_Contact As Contact = New Contact
        If m_Last <> "" Then m_Contact.LastName = New Identifier With {.Value = m_Last}
        If m_First <> "" Then m_Contact.FirstName = New Identifier With {.Value = m_First}
        If m_Middle <> "" Then m_Contact.MiddleName = New Identifier With {.Value = m_Middle}
        If m_Suffix <> "" Then m_Contact.Suffix = New Identifier With {.Value = m_Suffix}
        iEntity.Contacts.Add(m_Contact)
        SignerComboBox.DataBind()
        SignerComboBox.SelectedIndex = SignerComboBox.Items.Count - 1
        'SignerComboBox.SelectedIndex = 3
    Else
        SignerContactSuffixTextBox.ErrorText = "Must have First or Last name."
        SignerContactSuffixTextBox.IsValid = False
    End If
End Sub

The key lines being these:

iEntity.Contacts.Add(m_Contact)
SignerComboBox.DataBind()
SignerComboBox.SelectedIndex = SignerComboBox.Items.Count - 1
'SignerComboBox.SelectedIndex = 3

The first line adds a new Contact to the collection.

The second line rebinds the ComboBox which results in the Selecting event from above being fired and getting the updated list including the new contact. Everything is still working fine. I can put a watch on SignerCombBox.Items and see that the new item is there after the bind. It was not there prior to the bind. All is good.

Then we come to the third line. This is where we have a problem. I expect this to select the last item in the list of items. The results of SignerComboBox.Items.Count is correct. We get a count back that does include the new item. What doesn't work, is that when we try to use that result to set the SelectedIndex property. It will not accept it. The fourth line is where I've tried to do everything manually to make sure I'm not losing my mind. Turns out I'm not. As you can see this line is commented out.

Even though I have added another line to Items and I can see that line in Items. It can not be set as selected using SelectedIndex. When I try to set it, it just remains set to whatever it was set to previously (in my case 0). I've also tried setting it with SignerComboBox.SelectedItem = SignerComboBox.Items(SignerComboBox.Items.Count - 1) or SignerComboBox.SelectedItem = SignerComboBox.Items(3) to no avail.

So, for example. With my test data I start out with 3 items in the collection. 0 - 2 are in Items. At this point I can set SignerComboBox.SelectedIndex to 0, 1, or 2. It works just fine. I then add another item. Rebind to get the updated Items. Check the Items. They now contain 4 items. 0, 1, 2, and 3 Set SignerComboBox.SelectedIndex = 3. It will开发者_StackOverflow中文版 not work. It will remain 0. Even though I know that item is in the Items list of the control. Set SignerComboBox.SelectIndex = 2. Works just fine. It will be set to 2.

It's as if it's deciding whether or not it's a valid index based on something other than it's own Items list. I even tried inserting the new one higher in the list and using an index that had already existed. Once that had been done though, that index would not be accepted.

I really don't understand it. I'm pretty sure it must be a bug. Even that doesn't make much sense to me though, because it seems like the kind of bug that would have been reported about a million times over already. So my only guess is that perhaps it has something to do with the fact that I'm handling the Selecting event of the DataSource and giving it a custom result. I don't see why that would matter, but I can't see how anything else is the least bit out of the ordinary.

I've been pounding on this problem for a while now and I'm out of ideas. So any help would be greatly appreciated.

Thanks, Tory

EDIT: I thought maybe I could phrase the question a bit more succintly.

Here's what it comes down to:

  1. Bind a combobox to a LinqDataSource based on a LinqToSql object that is related as a child to another LinqToSql object.

  2. Create an instance of the parent object.

  3. Use the LinqDataSource.Selecting event to use the contents of the parent object instance's child object collection, as the results of the LinqDataSource in the form of an IList(Of ChildObject).

  4. Show the contents of the ComboBox.

  5. Add a new Child object to the child object collection of the parent object instance.

  6. Databind the ComboBox again in order to pick up the newly added member of the collection.

  7. Attempt to set the SelectedIndex of the ComboBox to the index of the newly created member of it's contents, using something like this:

    SignerComboBox.SelectedIndex = SignerComboBox.Items.Count - 1

  8. You will not be able to.

  9. It will show up in the ComboBox.Items list, but you will not be able to assign it's index to the SelectedIndex property.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜