using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using ASPWebPart = System.Web.UI.WebControls.WebParts;
using System.Xml;
using Microsoft.SharePoint.WebPartPages;
namespace Train.StudentQuery
{
public partial class StudentQueryUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(@"http://lwj:7004"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["班级信息"];
SPListItemCollection items = list.Items;
ddlClass.Items.Add(new ListItem("所有班级", ""));
foreach (SPListItem item in items)
{
string className = item["班级名称"] == null ? "" : item["班级名称"].ToString();
ddlClass.Items.Add(new ListItem(className, className));
}
}
}
});
}
}
protected void btnQuery_Click(object sender, EventArgs e)
{
string query = getQuery();
execQuery(query);
}
protected void btnReset_Click(object sender, EventArgs e)
{
txtName.Text = "";
for (int i = 0; i < ddlClass.Items.Count; i++)
{
ddlClass.Items[i].Selected = false;
}
ddlClass.Items[0].Selected = true;
for (int i = 0; i < ddlSex.Items.Count; i++)
{
ddlSex.Items[i].Selected = false;
}
ddlSex.Items[0].Selected = true;
string query = getQuery();
execQuery(query);
}
/// <summary>
/// 获取查询字符串
/// </summary>
/// <returns></returns>
string getQuery()
{
string query = "";
string addCaml = "";
SPList list = SPContext.Current.Web.Lists["学员信息表"];
if (this.txtName.Text != "")
{
addCaml = @"<Where>
<Contains>
<FieldRef Name='" + list.Fields["姓名"].InternalName + @"' />
<Value Type='Text'>" + txtName.Text + @"</Value>
</Contains>
</Where>";
query = createCaml("And", query, addCaml);
}
if (this.ddlSex.SelectedItem.Value != "")
{
addCaml = @"<Where>
<Eq>
<FieldRef Name='" + list.Fields["性别"].InternalName + @"' />
<Value Type='Text'>" + this.ddlSex.SelectedItem.Value + @"</Value>
</Eq>
</Where>";
query = createCaml("And", query, addCaml);
}
if (this.ddlClass.SelectedItem.Value != "")
{
addCaml = @"<Where>
<Eq>
<FieldRef Name='" + list.Fields["所在班级"].InternalName + @"' />
<Value Type='Text'>" + this.ddlClass.SelectedItem.Value + @"</Value>
</Eq>
</Where>";
query = createCaml("And", query, addCaml);
}
//Response.Write("<script>alert('"+query+"')</script>");
return query;
}
string createCaml(string logicType, string caml, string addCaml)
{
string returnCaml = "";
if (caml == "")
{
XmlDocument meXml = new XmlDocument();
meXml.LoadXml(addCaml);
XmlNode mewhere = meXml.SelectSingleNode("Where");
returnCaml = "<Where>"
+ mewhere.InnerXml
+ "</Where>";
}
else
{
XmlDocument docXml = new XmlDocument();
docXml.LoadXml(caml);
XmlNode where = docXml.SelectSingleNode("Where");
XmlDocument meXml = new XmlDocument();
meXml.LoadXml(addCaml);
XmlNode mewhere = meXml.SelectSingleNode("Where");
returnCaml = "<Where><" + logicType + ">" +
where.InnerXml +
mewhere.InnerXml +
"</" + logicType + "></Where>";
}
return returnCaml;
}
void execQuery(string query)
{
ASPWebPart.WebPartCollection webParts = ASPWebPart.WebPartManager.GetCurrentWebPartManager(this.Page).WebParts;
foreach (ASPWebPart.WebPart wp in webParts)
{
if (wp is XsltListViewWebPart)
{
XsltListViewWebPart xsltListViewWebPart = wp as XsltListViewWebPart;
if (xsltListViewWebPart != null)
{
string XmlDefinition = xsltListViewWebPart.XmlDefinition;
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(XmlDefinition);
XmlNode ViewNode = xmldoc.SelectSingleNode("View");
((XmlElement)ViewNode).SetAttribute("Scope", "Recursive");
XmlNode queryNode = xmldoc.SelectSingleNode("View/Query");
queryNode.InnerXml = query + queryNode.InnerXml;
XmlNode AggregationsNode = xmldoc.SelectSingleNode("View/Aggregations");
if (AggregationsNode == null)
{
XmlNode newAggregationsNode = xmldoc.CreateElement("Aggregations");
((XmlElement)newAggregationsNode).SetAttribute("Value", "Off");
ViewNode.AppendChild(newAggregationsNode);
}
else
{
((XmlElement)AggregationsNode).SetAttribute("Value", "Off");
}
//XmlNode ToolbarNode = xmldoc.SelectSingleNode("View/Toolbar");
//((XmlElement)AggregationsNode).SetAttribute("Type", "Freeform");
xsltListViewWebPart.XmlDefinition = xmldoc.OuterXml;
xsltListViewWebPart.AsyncRefresh = true;
}
}
}
}
}
}