I have an employee list in Sharepoint that has 2 lookup fields(manager and department).
I want to know how to bind the lookup field values to the asp.net DropDownList.
So I get a dropdown with SharePoint list data bound to the gridview.
I already can see a post relevant to this but that is using SP 2010. I am in need of code for SP 2013
Thanks! Please help!
If I need for DropDownList to fetch data from SP list, I use something like this
using (SPSite site = new SPSite(http://sharepointSiteWithList))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["listName"];
dd.DataSource = list.Items;
dd.DataValueField = "Department";
dd.DataTextField = "Department";
dd.DataBind();
}
}
It will be done with 2013 version.
.Aspx Code:
<asp:DropDownList ID="drpbind" runat="server" AutoPostBack="true">
</asp:DropDownList>
Add Namespace:
Using microsoft.SharePoint;
.Cs Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using(SPSite site = new SPSite("http://yoursharepointsite"))
{
using(SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Authors"];
drpbind.DataSource = list.Items;
drpbind.DataValueField = "Title";
drpbind.DataTextField = "Title";
drpbind.DataBind();
}
}
}
}
Ref: More detailed steps .
Ref: To bind GridView .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.