gpt4 book ai didi

c# - 如何使用 C# 将列表项列绑定(bind)到共享点中的下拉列表

转载 作者:行者123 更新时间:2023-11-30 12:49:16 26 4
gpt4 key购买 nike

我有两个列表,分别称为“类(class)”和“讲师”。 “类(class)”列表包含以下列。

CourseName   Duration   
----------------------
Sharepoint 60days
MSBI 45days
.Net 90days
Java 50days

讲师列表包含以下列

 Instructor   Course   
---------------------
John Sharepoint
Mike MSBI
Bob Java

我想将“CourseName”列添加到下拉列表中,这应该在 webpart 中实现。当我们从该下拉列表中选择任何类(class)时,我们应该在标签中显示教师的姓名。最初,我尝试向 Web 部件添加一个下拉列表,以使用以下代码显示 CourseName 列。但是我创建失败了。

DropDownList drpList;
protected override void CreateChildControls()
{
drpList = new DropDownList();
SPSite site = SPContext.Current.Site;
SPWeb web = site.RootWeb;

SPList list1 = web.Lists["Courses"];
var listitems = list1.Fields["Course Name"];
drpList.DataSource = listitems;
drpList.DataTextField = "Course Name";
drpList.DataValueField = "Course Name";
drpList.DataBind();
Controls.Add(drpList);
}

谁能建议我正确的方法!!

新实现。我尝试使用以下代码

DropDownList drpList;
protected override void CreateChildControls()
{
drpList = new DropDownList();
SPSite site = SPContext.Current.Site;
SPWeb web = site.RootWeb;
ArrayList myarr = new ArrayList();
myarr.Add(1);
myarr.Add(2);
SPSiteDataQuery dataquery = new SPSiteDataQuery();
dataquery.Lists = string.Format("<Lists><List ID={0} /></Lists>",web.Lists["Courses"].ID);
dataquery.ViewFields = "<FieldRef Name=\"Course Name\"/>";
DataTable dt = web.GetSiteData(dataquery);
drpList.DataTextField = "Course Name";
drpList.DataValueField = "Course Name";
drpList.DataSource = dt;
drpList.DataBind();
Controls.Add(drpList);
}

出现下拉列表但没有数据。我认为 CAML 查询中存在错误。任何人都可以纠正我!!

最佳答案

Lukasz M 所指的帖子是我自己写的。

首先,您需要有两个单独的列表吗?你不能只有一个名为 Courses 的列表,其中包含以下列:*CourseName、Duration、Instructor *?****** 然后你可以应用 CAML 查询基本上做这样的事情:如果 CourseName = "SharePoint",那么从 Instructor Column 中显示值 John?如果您按照我的描述设置了此列表(一个列表中的所有 3 列),则代码将如下所示(放置在 Page_Load 事件中):

    SPListItemCollection itemCol = null;
SPSite site = new SPSite("http://yoursharepointsite");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["Courses"]; //Name of your List with Courses
SPQuery qryCourse = new SPQuery();
//This checks what you have selected in the drop-down list (assuming it's called dropList)
qryCourse.Query = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + dropList.SelectedItem.Text + "</Value></Eq></Where>";
qryCourse.ViewFields = "<FieldRef Name='Instructor'/>"; //You want the Instructor Column to display

try
{
itemCol = list.GetItems(qryCourse);
foreach (SPListItem item in itemCol)
{
//Display the Instructor value in your label
lblInstructorName.Text = item["Instructor"].ToString();
}
}
catch (NullReferenceException)
{
lblInstructorName.Text = "Some Kind of Error!";
}

请注意,您仍然需要使用我之前帖子中的代码 Retrieve SharePoint List Data and bind this to a dropdownlist 绑定(bind) dropList 字段,以便它从名为 Courses 的 Sharepoint 列表中检索其值,正如 Lukasz M 所建议的那样。这也将进入 Page_Load 事件(在我上面写的代码之前)。希望对 Mihir 有所帮助!

关于c# - 如何使用 C# 将列表项列绑定(bind)到共享点中的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12110562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com