如何使用C#将List Item列绑定到sharepoint中的下拉列表

时间:2012-08-24 13:38:52

标签: c# sharepoint

我有两个名为“课程”,“教师”的名单。 “课程”列表包含以下列。

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

教师列表包含以下列

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

我想将“CourseName”列添加到下拉列表中,该列表应该在webpart中实现。 当我们从该下拉列表中选择任何课程时,我们应该在标签中显示教师的姓名。 最初,我尝试将下拉列表添加到webpart,以显示带有以下代码的 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查询中存在错误。请任何人纠正我!!

3 个答案:

答案 0 :(得分:1)

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

首先,您需要有两个单独的列表吗?您是否只有一个名为课程的列表,其中包含以下列: * CourseName,Duration,Instructor * ?******然后您可以应用CAML查询这基本上是这样的:如果CourseName =“SharePoint”,那么从教师列显示值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 来绑定 dropList 字段,以便从中检索其值Lukasz M建议的Sharepoint列表名为 Courses 。这也将出现在 Page_Load 事件中(在我上面的代码之前)。希望能帮助Mihir!

答案 1 :(得分:0)

在SO上查看这个问题:Retrieve SharePoint List Data and bind this to a dropdownlist。它似乎完全按照你的意愿工作。根据该代码,下拉列表的数据源应设置如下:drpList.DataSource = list1.Items;。另请注意,代码已添加到Page_Load方法中。

答案 2 :(得分:0)

尝试使用提供者和消费者webpart。 这可能是你的答案

Source

感谢

相关问题