Get Data in a Generic List after the Post Back

First you have to create a class 

    [Serializable]
    public class Keyword
    {
     
        [Serializable]
        public class Item
        {
            public string KeywordId { get; set; }
        }

        [Serializable]
        public class KeywordList
        {
            public List<Item> KeywordId()
            {
                return null;
            }
        }

    }

   
   Then after in a page .cs File

  public string ItemsValues = "Keyword";
      
  public List<Keyword.Item> Items
        {
            get
            {
                if (!(ViewState[ItemsValues] is List<Keyword.Item>))
                {
                    ViewState[ItemsValues] = new List<Keyword.Item>();
                }
                return (List<Keyword.Item>)ViewState[ItemsValues];
            }
       }



Adding item in List 

  Items.Add(new Keyword.Item
            {
                KeywordId = e.Entry.Value

            });

Removing item in List



  Items.Remove(new Keyword.Item
            {
                KeywordId = e.Entry.Value

            });


Retrive data after the Postback

          List<Keyword.Item> Keyword_List= (List<Keyword.Item>)ViewState[ItemsValues];

           if (Keyword_List.Count > 0)
            {
                foreach (var data in Keyword_List)
                {
                    string id = data.KeywordId;
                }
            }






Comments

Popular posts from this blog

Validate Mobile Number with 10 Digits in ASP.Net