Retrieve SharePoint List Data and bind this to a dropdownlist

不想你离开。 提交于 2019-11-30 17:33:59

问题


I'm fairly new to SharePoint so apologies in advance for sounding like a 'Newbie'.

I have created a simple Webpart, which uses a Web User Control - [.ascx file] to provide all the controls for the Webpart. On the .ascx file, there is a DropDownList which is hard-coded at the moment and works well in the Webpart (within a SharePoint site).

However, I want the DropDownList on the .ascx file to be bound to a particular Column of a SharePoint List, so that when I update that column of the SharePoint List, the DropDownList reflects the update automatically.

Do any of you kind folk have any ideas on how to achieve this please?

Thank you very much in advance,

Ash 8-)

(p.s. Happy New Year to you All !)


回答1:


I found the answer within minutes of posting the above article (typical).

The solution is to place the following code in the Page_Load event of the .ascx.cs (code-behind) file:

if (!Page.IsPostBack)
        {
            using (SPSite site = new SPSite("http://yoursharepointsite"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["NameOfYourList"];
                    dropSite.DataSource = list.Items;
                    dropSite.DataValueField = "Title"; // List field holding value - first column is called Title anyway!
                    dropSite.DataTextField = "Title"; // List field holding name to be displayed on page 
                    dropSite.DataBind();
                }
            }
        }

I found the solution here:

http://blogs.msdn.com/mattlind/archive/2008/02/12/bind-a-asp-dropdownlist-to-a-sharepoint-list.aspx

Thanks,

Ash



来源:https://stackoverflow.com/questions/1999269/retrieve-sharepoint-list-data-and-bind-this-to-a-dropdownlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!