Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Friday, 13 June 2014

Common DropDown Filling Code

 protected void FillDropDown()
        {
            try
            {
                string Query = @"<Where><IsNotNull><FieldRef Name='Title' /></IsNotNull></Where>";
                DataTable dt = GetDropdownItems("ListName", Query);
                BindDropdown(ddl, "Title", "Title", "", dt);
                ListItem objListItem = new ListItem("-- Please Select --", "");
                ddl.Items.Insert(0, objListItem);
            }
            catch (Exception ex)
            {
                ExceptionLog(ex);
            }
        }

private DataTable GetDropdownItems(string strListName, string strQuery)
        {
            DataTable dtSegment = null;
           
                SPQuery query = new SPQuery();
                query.Query = strQuery;
                SPSite spCurSite = SPContext.Current.Site;
                SPWeb web = spCurSite.OpenWeb();
                SPList list = web.Lists[strListName];
                SPListItemCollection itemColl = list.GetItems(query);
                dtSegment = itemColl.GetDataTable();

            return dtSegment;
        }

        private void BindDropdown(DropDownList ddl, string strDataTextField, string strDataValueField, string strDefaultVal, DataTable dtDropdownValues)
        {
           
                ddl.DataSource = dtDropdownValues;
                ddl.DataTextField = strDataTextField;
                ddl.DataValueField = strDataValueField;
                ddl.DataBind();
        }

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.