为List配置一个搜索按钮
生活随笔
收集整理的這篇文章主要介紹了
为List配置一个搜索按钮
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
查詢前的List首頁,只有一個cusotmer customer下面有個Project文件夾和Cusotmer Complaint文件 其中Customer Complaint.txt文件是有多個屬性的 現在開始針對文檔庫中所有的文件 和文件夾進行模糊查詢 前段代碼 <%@?Control?Language="C#"?AutoEventWireup="true"?CodeBehind="SearchList.ascx.cs"?Inherits="MySharePointDev.Public.SearchList"?%><table>
????<tr>
????????<td>
????????????<asp:Label?ID="labKeyWord"?runat="server"?Text="KeyWord:"?Font-Bold="True"?></asp:Label>
????????????<asp:TextBox?ID="tbKeyWord"?runat="server"?></asp:TextBox>
????????????<asp:Button?ID="btnSearch"?runat="server"?onclick="btnSearch_Click"?
????????????????Text="Search"?/>
????????</td>
????</tr>
</table>
?
后端代碼 using?System;using?System.Collections.Generic;
using?System.Linq;
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?Microsoft.SharePoint;
using?Microsoft.SharePoint.WebPartPages;
using?System.Xml;
using?System.Web.UI.WebControls.WebParts;
namespace?MySharePointDev.Public
{
????public?partial?class?SearchList?:?BaseSPWebPart
????{
????????private?string?_QueryFields?=?"Title;";
????????[Personalizable(PersonalizationScope.Shared)]
????????[WebBrowsable]
????????[WebDisplayName("查詢的字段顯示名,以分號分隔開")]
????????public?string?QueryFields
????????{
????????????get?
????????????{
????????????????if?(_QueryFields.EndsWith(";"))//防止以;結束
????????????????{
????????????????????return?_QueryFields.Substring(0,?_QueryFields.Length?-?2);
????????????????}
????????????????else
????????????????{
????????????????????return?_QueryFields;
????????????????}
????????????}
????????????set?{?_QueryFields?=?value;?}
????????}
????????private?SPList?List
????????{
????????????get
????????????{
????????????????return?SPContext.Current.List;
????????????}
????????}
????????protected?void?Page_Load(object?sender,?EventArgs?e)
????????{
????????????//this.ExportMode?=?WebPartExportMode.All;
????????????tbKeyWord.ToolTip?=?"Please?input:?"?+?QueryFields;
????????}
????????///?<summary>
????????///?search
????????///?</summary>
????????///?<param?name="sender"></param>
????????///?<param?name="e"></param>
????????protected?void?btnSearch_Click(object?sender,?EventArgs?e)
????????{
????????????if?(this.tbKeyWord.Text?!=?"")
????????????{
????????????????string[]?fields?=?QueryFields.Split(';');
????????????????string?cmal?=?"";
????????????????foreach?(string?field?in?fields)
????????????????{
????????????????????if?(cmal?==?"")?//第一個參數
????????????????????{
????????????????????????cmal?=?string.Format(@"<Contains>
??????????????????????????????????????????????????????????????<FieldRef?Name='{0}'?/>
??????????????????????????????????????????????????????????????<Value?Type='{1}'>{2}</Value>
???????????????????????????????????????????????????????????</Contains>
???????????????????????????????????????????????????????????",?List.Fields[field].InternalName,?List.Fields[field].Type.ToString(),?this.tbKeyWord.Text.ToString());
????????????????????}
????????????????????else
????????????????????{
????????????????????????cmal?=?"<Or>"?+?cmal?+?string.Format(@"<Contains>
??????????????????????????????????????????????????????????????<FieldRef?Name='{0}'?/>
??????????????????????????????????????????????????????????????<Value?Type='{1}'>{2}</Value>
???????????????????????????????????????????????????????????</Contains></Or>
???????????????????????????????????????????????????????????",?List.Fields[field].InternalName,?List.Fields[field].Type.ToString(),?this.tbKeyWord.Text.ToString());
????????????????????}
????????????????????
????????????????}
????????????????cmal?=?"<Where>"?+?cmal?+?"</Where>";
????????????????this.SetCurrentListViewSchemaQuery(cmal);
????????????????
????????????}
????????}
????????///?<summary>
????????///?設置當前l(fā)ist的視圖?schema?query?
????????///?</summary>
????????///?<param?name="cmal"></param>
????????private?void?SetCurrentListViewSchemaQuery(string?cmal)
????????{
????????????if?(!string.IsNullOrEmpty(cmal))
????????????{
????????????????string?str?=?"{"?+?this.List.ID.ToString()?+?"}";
????????????????ControlCollection?cc?=?this.Page.Controls;
????????????????GetControls(ref??cc,?str,?cmal);
????????????}
????????}
????????///?<summary>
????????///?遍歷所有頁面中所有控件
????????///?</summary>
????????///?<param?name="cs">頁面控件集</param>
????????///?<param?name="str"></param>
????????///?<param?name="cmal"></param>
????????private?void?GetControls(ref?ControlCollection?cs,?string?str,?string?cmal)
????????{
????????????foreach?(Control?webPart?in?cs)
????????????{
????????????????if?(webPart.HasControls())
????????????????{
????????????????????ControlCollection?cc?=?webPart.Controls;
????????????????????GetControls(ref??cc,?str,?cmal);
????????????????}
????????????????if?(webPart?is?ListViewWebPart)
????????????????{
????????????????????ListViewWebPart?listViewWebPart?=?(ListViewWebPart)webPart;
????????????????????if?(string.Compare(listViewWebPart.ListName,?str,?true)?!=?0)
????????????????????{
????????????????????????continue;
????????????????????}
????????????????????if?(string.IsNullOrEmpty(cmal))
????????????????????{
????????????????????????listViewWebPart.ListViewXml?=?this.List.Views[new?Guid(listViewWebPart.ViewGuid)].HtmlSchemaXml;
????????????????????}
????????????????????else
????????????????????{
????????????????????????//申明個新的XmlDocument,將listViewWebPart的ListViewXml裝載進去
????????????????????????XmlDocument?xmlDocument?=?new?XmlDocument();
????????????????????????xmlDocument.LoadXml(listViewWebPart.ListViewXml);
????????????????????????//用cmal替換掉原有的查詢語句
????????????????????????this.ChangeSchemaXmlCaml(xmlDocument,?cmal);
????????????????????????listViewWebPart.ListViewXml?=?xmlDocument.InnerXml;
????????????????????}
????????????????}
????????????}
????????}
????????///?<summary>
????????///?move?where?去除外部的Where和/Where
????????///?</summary>
????????///?<param?name="q"></param>
????????///?<returns></returns>
????????private?string?GetInnerQuery(string?q)
????????{
????????????XmlDocument?docuemnt?=?new?XmlDocument();
????????????docuemnt.LoadXml(q);
????????????return?docuemnt.DocumentElement.InnerXml;
????????}
????????///?<summary>
????????///?change?schema?xml?query?
????????///?</summary>
????????///?<param?name="xmlDocument"></param>
????????///?<param?name="query"></param>
????????private?void?ChangeSchemaXmlCaml(XmlDocument?xmlDocument,?string?query)
????????{
????????????if?(!string.IsNullOrEmpty(query))
????????????{
????????????????string?innerXml?=?this.GetInnerQuery(query);
????????????????if?(innerXml?!=?"")
????????????????{
????????????????????//獲得Query部分的XmlNode
????????????????????XmlNode?node?=?xmlDocument.DocumentElement.SelectSingleNode("Query");
????????????????????XmlNode?oldChild?=?node.SelectSingleNode("Where");
????????????????????//如果有Where,去除它
????????????????????if?(oldChild?!=?null)
????????????????????{
????????????????????????node.RemoveChild(oldChild);
????????????????????}
????????????????????//新建一個Where,并將我們的query的主體內容賦值給其InnerXml,并在QuerNode中添該Node
????????????????????XmlNode?newChild?=?xmlDocument.CreateElement("Where");
????????????????????newChild.InnerXml?=?innerXml;
????????????????????node.AppendChild(newChild);
????????????????????//將搜索文件的類型修改為list中的所有文件和文件夾,默認為只搜索當前目錄下的文件
????????????????????if?(xmlDocument.DocumentElement.Attributes["Scope"]?==?null)
????????????????????{
????????????????????????XmlAttribute?attScope?=?xmlDocument.CreateAttribute("Scope");
????????????????????????attScope.Value?=?"RecursiveAll";
????????????????????????xmlDocument.DocumentElement.Attributes.Append(attScope);
????????????????????}
????????????????????else
????????????????????{
????????????????????????xmlDocument.DocumentElement.Attributes["Scope"].Value?=?"RecursiveAll";
????????????????????}
????????????????????????
????????????????????????
????????????????????//替換XML中的ViewEmpty部分,該部分顯示沒有數據的時候的內容
????????????????????xmlDocument.DocumentElement.SelectSingleNode("ViewEmpty").InnerXml?=?"<HTML><![CDATA[<font?color='red'><b>No?Results?match?keyword!</b></font>]]></HTML>";
????????????????}
????????????}
????????}
????}
}
?
?
來源:http://www.cnblogs.com/ceci/archive/2009/12/02/1615592.html
轉載于:https://www.cnblogs.com/greeny/archive/2010/09/06/1819410.html
總結
以上是生活随笔為你收集整理的为List配置一个搜索按钮的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ajax 实现在WebForm中拖动控件
- 下一篇: 编译asp.net文件为dll文件