我有两个列表,分别称为“课程”、“讲师”。“课程”列表包含以下列。
CourseName Duration
----------------------
Sharepoint 60days
MSBI 45days
.Net 90days
Java 50days
讲师列表包含以下列
Instructor Course
---------------------
John Sharepoint
Mike MSBI
Bob Java
我想将“CourseName”列添加到下拉列表中,这应该在 webpart 中实现。当我们从该下拉列表中选择任何课程时,我们应该在标签中显示讲师的姓名。最初,我尝试向 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 查询中有错误。任何人都可以纠正我吗?