Bind Dropdownlist In SharePoint Using C#

In this article I have explained how to bind the SharePoint list columns into the Dropdownlist using C#.

Here are the steps

Open SharePoint team site.

customsite

Create a new list For example: “Authors”.

Add custom list

Create a custom column named “Name of the author”.

information into the list

Now add some information into the list.

authors

Open Microsoft Visual Studio.

Visual Studio

Create a new SharePoint 2013 Empty Project.

Webpart

Add a new visual Webpart into it.

ASPX Code:
  1. <asp:DropDownList ID="drpbind" runat="server" AutoPostBack="true">  
  2. </asp:DropDownList>  
ADD namespaces
  1. Using microsoft.SharePoint;  
CS Code:
  1. protected void Page_Load(object sender, EventArgs e)   
  2. {  
  3.     if (!Page.IsPostBack)   
  4.     {  
  5.         using(SPSite site = new SPSite("http://winserver/sites/customsite/"))   
  6.         {  
  7.             using(SPWeb web = site.OpenWeb())   
  8.             {  
  9.                 SPList list = web.Lists["Authors"];  
  10.                 drpbind.DataSource = list.Items;  
  11.                 drpbind.DataValueField = "Title"  
  12.                 drpbind.DataTextField = "Title"  
  13.                 drpbind.DataBind();  
  14.             }  
  15.         }  
  16.     }  
  17. }  
In the above code bind the title column.

Final result:

result