0

I took some code for displaying the content of a sharepoint 2010 list in a webpart based on a CAML query and modified it to display the same thing from two lists instead of one in a table. Now I want to further modify the code in order to get the number of incomplete tasks across both lists. to do that I added counters to the loops that populate the table and stored the result in another variable c. Now, the table is populated fine and does what i want it to do, but the value of the variable c is not displayed... can anyone help? I'll be really thankfull. Here is the code:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SharePoint365.WebParts.OutstandingTasks
{
  [ToolboxItemAttribute(false)]
  public class OutstandingTasks : WebPart
  {
    int a; // HERE (1)
    int b; // HERE (1)
    int c; // HERE (1)
    protected override void CreateChildControls()
    {
      /*int a;
      int b;
      int c;*/
      SPList taskList = SPContext.Current.Web.Lists["test list 1"];
      SPList taskList1 = SPContext.Current.Web.Lists["test list 2"];

      SPQuery query = new SPQuery();
      query.Query = "<Where><Neq><FieldRef Name='Status' />";
                   +"<Value Type='Text'>Completed</Value></Neq></Where>";
      query.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Status'/>";

      SPListItemCollection tasks = taskList.GetItems(query);
      SPListItemCollection tasks1 = taskList1.GetItems(query);
      string htmlTable = "<table border='1'>";  
      htmlTable += "<tr><td>Title</td><td>Status</td></tr>";  

      foreach (SPListItem aTask in tasks)  
      {  
        htmlTable += string.Format("<tr><td>{0}</td><td>{1}</td></tr>",
                       aTask["Title"], aTask["Status"]);
        a++;  // HERE (2)
      }
      foreach (SPListItem aTask in tasks1)
      {
        htmlTable += string.Format("<tr><td>{0}</td><td>{1}</td></tr>",
                       aTask["Title"], aTask["Status"]);
        b++; // HERE (3)
      }

      htmlTable += "</table>";  

      c = a + b;            // HERE (4)
      Console.WriteLine(c); // HERE (4)

      LiteralControl tableControl = new LiteralControl(htmlTable);  
      this.Controls.Add(tableControl); 
    }
  }
}
4

2 回答 2

1

您不能使用 Console.WriteLine 来显示 c。您需要像使用其他字符串一样使用 LiteralControl:

c = a + b;
LiteralControl totals = new LiteralControl(c.ToString());
this.Controls.Add(totals);  
于 2012-09-04T06:11:49.373 回答
0

也许你得到了答案,但是。您过滤两个表的方法是错误的。您需要使用 SPSiteDataQuery对象,我们可以使用它来查询多个共享点列表。

SPContext.Current.Web.GetSiteData(SPSiteDataQuery 对象);

按照以下链接了解更多信息 http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsitedataquery.aspx

于 2012-09-13T18:37:44.477 回答