Ömer Çelik

Yazılımcının Günlüğü

Custom list dropdownlist SPDataSource kullanımı

public static void GetDataKategoriler(ref DropDownList Liste)
        {          
using (SPSite site = new SPSite("http://www.omercelik.com.tr"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Kategoriler"];
                    SPQuery query = new SPQuery();
                    query.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"ID\" />";
                    query.ViewFieldsOnly = true;
                    SPListItemCollection items = list.GetItems(query);
                    Liste.Items.Clear();
                    Liste.Items.Add(new ListItem("Kategori Seçiniz", "0"));
                    foreach (SPListItem item in items)
                    {
                        Liste.Items.Add(new ListItem(item["Title"].ToString(), item["ID"].ToString()));
                    }
                }
            }
         }

Diğer bir yöntem

using (SPSite site = new SPSite("http://www.omercelik.com.tr"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList myList = web.Lists["kategoriler"];
                    SPDataSource dataSource = new SPDataSource();
                    dataSource.List = myList;
                    dataSource.DataSourceMode = SPDataSourceMode.List;
                    dataSource.IncludeHidden = false;
                    dataSource.Scope = SPViewScope.Recursive;
                    dataSource.SelectCommand = "<View><Query><OrderBy><FieldRef Name='Title'/></OrderBy></Query></View>";
                    dataSource.UseInternalName = true;
                    Parameter listIdParam = new Parameter("ListID");
                    listIdParam.DefaultValue = myList.ID.ToString("B").ToUpper();
                    dataSource.SelectParameters.Add(listIdParam);
                    DropDownList ddl = new DropDownList();
                   dll.DataSource = dataSource;
                   dll.DataTextField = "Title";
                   dll.DataBind();
               }
         }

Loading