Sunday, March 31, 2013

ObservableCollection in ASP.NET

This simple example will help you to understand and how to use the "ObservableCollection" class list and INotifyPropertyChanged interface.Both are playing important role in WPF and Silverlight application implementation and especially on MVVM pattern. Where an entity class updating the IEnumarable and collection list,INotifyPropertyChanged event trigger to update the View Model. Here is straight forward workaround.I have a class with three members like FirstName,LastName and City. As soon as you hit the button save it will bind to the DataGrid control in Silverlight. I did in SharePoint 2013.
       
        public MainPage()
        {
            InitializeComponent();
            List Name = new List();
            Name.Add("Chennai");
            Name.Add("Banglore");
            Name.Add("Mumbai");
            autoCom.ItemsSource = Name;
           
            
        }
        ClientContext context;
        Web web;
        private delegate void UpdateUI();
        
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            context = ClientContext.Current;
             web = context.Web;
             context.Load(web);
             context.ExecuteQueryAsync(Onsuccess, OnFailure);
           
        }
        public void Onsuccess(object sender, ClientRequestSucceededEventArgs e)
        {
            UpdateUI obj = ShowResult;
            this.Dispatcher.BeginInvoke(obj);

        }
        public void OnFailure(object sender, ClientRequestFailedEventArgs e)
        {
            MessageBox.Show(e.StackTrace.ToString());
        }
        public void ShowResult()
        {
           
      
            ObservableCollection list = new ObservableCollection();
            list.Add(new observableList(txtFirst.Text, txtLastName.Text,autoCom.Text));
         
            MyGrid.ItemsSource = list;
         
        }
Create the class and inherit with INotifyPropertyChanged interface.
 public class observableList : INotifyPropertyChanged
    {
        private string fName;
        private string lName;
        private string city;
        public string FirstName
        {
            get { return fName; }
            set { fName = value;
            NotifyPropertyChanged("FirstName");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public string LastName { get { return lName; } set { lName = value; NotifyPropertyChanged("LastName"); } }
        public string City { get { return city; } set { city = value; NotifyPropertyChanged("City"); } }
        public void NotifyPropertyChanged(string param)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(param));
            }
        }
        public observableList(string f,string l,string c)
        {
            this.fName = f;
            this.lName = l;
            this.city = c;
        }


        
    }