Monday, September 29, 2014

How to bind Asp DropDownList Value and text

In order to bind Asp DropDownList value and text field use the following method

lets day DroDownList id is "ddl"

 List<taqi> objlist = new List<taqi>();
                taqi objt = new taqi();
                objt.Id = "2";
                objt.Name = "Taqi";
                objlist.Add(objt);   // adding item in list

                objt = new taqi();
                objt.Id = "1";
                objt.Name = "Ali";
                objlist.Add(objt);   // adding item in list
                                             // so we have added two items in list now we will bind dropdownlist

                ddl.DataSource = objlist;  
                ddl.DataTextField = "Name";    //Name field is define in the class called taqi
                ddl.DataValueField = "Id";        // Id field is define in the class called taqi
                ddl.DataBind();                        // finally we bind the dropdownlist

For your convenience here is the class source code

  public class taqi
    {

        private string id;

        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

I hope it was informative for you and I would like to thank you reading.

 

No comments:

Post a Comment