selectedvalue on page load

5 posts, 0 answers
  1. Alex Wu
    Alex Wu avatar
    31 posts
    Member since:
    Dec 2009

    Posted 13 Jan 2010 Link to this post

    I have a parent table Address and a child table States. Upon page load an address record is retrieved and displayed on a dataform. In the address dataform has a combobox box that displays all states in alphabetical order. A state should be selected base on the address record's foreign key State's value. This isn't working if the address's state is set to a state begins with a later character like "Utah". If the state is "Alaska" then it works. I am assuming it's because it takes time to load the combobox and when the databinding attempts to assign a value to the state combobox upon initial page load the combobox is not done loading so the value isn't there yet. how to I solve this issue?
  2. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 15 Jan 2010 Link to this post

    Hi Alex,

    Thank you for your question.

    You are right about that it takes time to load the ComboBox and the databinding fails because the ComboBox is not done loading. When RadComboBox has no items, but you set its SelectedValue (SelectedItem/SelectedIndex) property, it keeps the value until its Items collection changes and then tries to select the specified item. If you add the items one by one and the item that should be selected is not the first, the selection will fail and RadComboBox will not try to select items again. The workaround is to set all items at once, for example, to put them in a collection. That will avoid the issue with the missing selected item ComboBox.
    Attached you will find a sample running project for reference.

    If you need further assistance please feel free to contact us again.


    All the best,
    Konstantina
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Alex Wu
    Alex Wu avatar
    31 posts
    Member since:
    Dec 2009

    Posted 15 Jan 2010 Link to this post

    Databinding with ViewModel is exactly what I did but it doesn't work. I don't know how to attach none image files but I have simple code that you can download from .  It's in VS2010 Beta and SQL Server 2008

    Thank you for your help
  4. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 21 Jan 2010 Link to this post

    Hello Alex,

    Here is the code snippet where I think the problem is:

    public class GeneralVM
    {
     private MyContext _vmContext = new MyContext();
     private static IEnumerable<State> _states = null;

     public IEnumerable<State> States
     {
      get
      {
       if (_states == null)
       {
        _vmContext.Load(_vmContext.GetStatesQuery());
        _states = (IEnumerable<State>)_vmContext.States;
       }
       return _states;
      }
     }
    }


    The States property of MyContext is an empty ObservableCollection, that is filled with items when they come from the server, one by one (hence the problem we described in our previous reply). You need to change the code like this:

    public class GeneralVM : INotifyPropertyChanged
    {
     private MyContext _vmContext = new MyContext();
     private static IEnumerable<State> _states = null;

     public IEnumerable<State> States
     {
      get
      {
       if (_states == null)
       {
        _vmContext.Load(_vmContext.GetStatesQuery(), (o) =>
        {
         _states = o.Entities;
         this.OnPropertyChanged("States");
        }, null);
       }
       return _states;
      }
     }

     private void OnPropertyChanged(string propertyName)
     {
      if (this.PropertyChanged != null)
      {
       this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
     }
     public event PropertyChangedEventHandler PropertyChanged;
    }


    All the best,
    Valeri Hristov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Alex Wu
    Alex Wu avatar
    31 posts
    Member since:
    Dec 2009

    Posted 28 Jan 2010 Link to this post

    I did as you said but it's still not working. Please see the updated version of the simple code at http://awu.dnsalias.com/temp

    Thank you
Back to Top