【问题标题】:How to Bind List Item column to drop down list in sharepoint using C#如何使用 C# 将列表项列绑定到共享点中的下拉列表
【发布时间】:2012-08-20 02:04:34
【问题描述】:

我有两个列表,分别称为“课程”和“讲师”。 “课程”列表包含以下列。

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 查询中有错误。哪位大神可以指点一下!!

【问题讨论】:

    标签: c# sharepoint


    【解决方案1】:

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

    【讨论】:

    • @Lukasz.. 它不工作。在发布这个问题之前,我首先尝试了那个。
    • 您得到什么错误或描述使用此代码时究竟是什么不起作用(如我链接到的答案中所述)?你把这段代码放在Page_Load 方法中了吗?如果它为某人工作而不为你工作,那么在提供的代码或数据中可能存在差异,所以我试图找出它是什么区别:)。
    • 再次查看您的问题...您使用的是 SharePoint 2007 还是 2010?
    • 你有没有调试过这段代码?如果没有,请验证在drpList.DataSource = list.Items; 行之后的list.Items 中是否有任何项目。值得检查一下,因为 SharePoint 中对该特定列表的访问限制可能会阻止您的用户访问该列表(或者稍后只需验证您是否需要对该列表的权限)。
    【解决方案2】:

    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 来绑定 dropList 字段,以便它从 Sharepoint 列表中检索其值正如 Lukasz M 所建议的那样,将其称为 Courses。这也将出现在 Page_Load 事件中(在我上面写的代码之前)。希望对米希尔有所帮助!

    【讨论】:

    • 嗨 Mihir - 没有收到您的回复。我认为您已经解决了问题?
    【解决方案3】:

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

    Source

    谢谢

    【讨论】:

      猜你喜欢
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多