检索 SharePoint 列表数据并将其绑定到下拉列表

2023-11-29

我对 SharePoint 相当陌生,因此提前为听起来像“新手”而道歉。

我创建了一个简单的 Web 部件,它使用 Web 用户控件 - [.ascxfile] 提供 Web 部件的所有控件。在 .ascx 文件上,有一个 DropDownList,目前是硬编码的,并且在 Web 部件(在 SharePoint 站点内)中运行良好。

但是,我希望 .ascx 文件上的 DropDownList 绑定到 SharePoint 列表的特定列,以便当我更新 SharePoint 列表的该列时,DropDownList 自动反映更新。

请问各位好心人对如何实现这一目标有什么想法吗?

预先非常感谢您,

Ash 8-)

(ps祝大家新年快乐!)


我在发布上述文章(典型)后几分钟内找到了答案。

解决办法就是将下面的代码放入页面加载事件的.ascx.cs(代码隐藏)文件:

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();
                }
            }
        }

我在这里找到了解决方案:

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

Thanks,

Ash

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

检索 SharePoint 列表数据并将其绑定到下拉列表 的相关文章