开发者

Retrieve SharePoint List Data and bind this to a dropdownlist

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 !)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜