Question

I have a SharePoint list that contain column 'District' with some data in it. It contains 4 value, District 1, District 2, District 3 and District 4.

I want to grab that value 'District' in the SharePoint list items and put it in my ASP Form as a checkbox to eventually make a filter on a Grid View.

If anyone can help me with grabbing the value of SharePoint List items and turning it into checkbox, would really appreciate it.

Était-ce utile?

La solution

In case, the District is a single line of text, try the below code

using (SPSite site = new SPSite("SiteURL"))
   {
     using (SPWeb web = site.OpenWeb())
      {
       SPList list = web.Lists["ListName"];
       SPQuery query = new SPQuery();
       query.Query = "<View><ViewFields><FieldRef Name='District'/></ViewFields></View>"; 
       SPListItemCollection Items = list.GetItems(query);
      // Bind Checkbox List
       foreach (SPListItem Item in Items)
          {
             //CheckBoxListID.Items.Add(item["District"]);
              CheckBoxListID.Items.Add(new ListItem(Item.Title));
          }
      }
}

In case, the District is your field is a choice field, Please check, Retrieve a SharePoint Choice Field Value Using Server Object Model C#

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top