Retrieve SharePoint List Data and bind this to a dropdownlist

前端 未结 1 1574
北恋
北恋 2021-01-06 09:00

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

相关标签:
1条回答
  • 2021-01-06 09:27

    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 讨论(0)
提交回复
热议问题