zoukankan      html  css  js  c++  java
  • 列表查询WebPart

    SmartListSearchWebPart
    简单列表查询WebPart.本代码只是一个简单的Demo,代码没有实际的价值,但是很有学习价值.
    在项目中可以得到很好的应用!


    输入key





    Code :

    using System;
    using System.Runtime.InteropServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Serialization;
    using System.Xml;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;

    namespace SmartListSearchWebPart
    {
        [Guid("34903418-36da-45cb-ae81-27cd82b628dd")]
        public class SmartListSearchWebPart : System.Web.UI.WebControls.WebParts.WebPart
        {
            public SmartListSearchWebPart()
            {
                this.ExportMode = WebPartExportMode.All;
            }

            private Button btnSearch;
            private TextBox tbKey;
            /// <summary>
            /// create child controls
            /// </summary>
            protected override void CreateChildControls()
            {
                btnSearch = new Button();
                btnSearch.Text = "查询";
                btnSearch.Click += new EventHandler(btnSearch_Click);
                this.Controls.Add(btnSearch);

                tbKey = new TextBox();
                this.Controls.Add(tbKey);

                base.CreateChildControls();
            }

            private SPList List
            {
                get
                {
                    return SPContext.Current.List;
                }
            }
            /// <summary>
            /// search
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void btnSearch_Click(object sender, EventArgs e)
            {
      
                if (this.tbKey.Text != "")
                {
                    string cmal = string.Format("<Where><Contains><FieldRef Name='Title' /><Value Type='Text'>{0}</Value></Contains></Where>", this.tbKey.Text.ToString());
                    this.SetCurrentListViewSchemaQuery(cmal);
                }
               

            }
            /// <summary>
            /// set current list view schema query
            /// </summary>
            /// <param name="cmal"></param>
            private void SetCurrentListViewSchemaQuery(string cmal)
            {
                if (!string.IsNullOrEmpty(cmal))
                {
                    string str = "{" +this.List.ID.ToString() +"}";

                   
                    foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in base.Zone.WebParts)
                    {
                        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 xmlDocument = new XmlDocument();
                                xmlDocument.LoadXml(listViewWebPart.ListViewXml);
                                this.ChangeSchemaXmlCaml(xmlDocument, cmal);
                                listViewWebPart.ListViewXml = xmlDocument.InnerXml;
                            }
                        }
                    }
                }
            }
            /// <summary>
            /// move 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 != "")
                    {
                        XmlNode node = xmlDocument.DocumentElement.SelectSingleNode("Query");
                        XmlNode oldChild = node.SelectSingleNode("Where");

                        if (oldChild != null)
                        {
                            node.RemoveChild(oldChild);
                        }

                        XmlNode newChild = xmlDocument.CreateElement("Where");
                        newChild.InnerXml = innerXml;
                        node.AppendChild(newChild);
                        xmlDocument.DocumentElement.SelectSingleNode("ViewEmpty").InnerXml = "<HTML><![CDATA[<font color='red'><b>AA Say:未找到符合查询条件的记录。</b></font>]]></HTML>";
                    }

                }
            }
            /// <summary>
            /// render
            /// </summary>
            /// <param name="writer"></param>
            protected override void Render(HtmlTextWriter writer)
            {
                writer.Write("<table><tr><td>");
                writer.Write("关键字:");
                tbKey.RenderControl(writer);
                btnSearch.RenderControl(writer);
                writer.Write("</td></tr></table>");
            }
        }
    }

    这个webPart 核心就是修改它的Scheam就OK
    listViewWebPart.ListViewXml = xmlDocument.InnerXml;(主要代码)

  • 相关阅读:
    IntelliJ IDEA 2019.2.3 x64版本设置Run Dashboard
    IntelliJ IDEA 使用Tomcat后在控制台出现乱码'中文乱码 “淇℃伅”“涓夋湀”
    window10家庭版管理员权限问题
    用navicat连接数据库报错:1130-host ... is not allowed to connect to this MySql server如何处理
    IntelliJ IDEA导入已有的项目的方法
    从养孩子谈谈 IO 模型(一)
    写作之路,以梦为马,不负韶华
    数据库核心:索引,你知道多少?
    面试:啥是数据倾斜?就是数据歪啦!
    ​面试:业务开发中你用到了哪些算法(续)?
  • 原文地址:https://www.cnblogs.com/liuyuhua/p/1700029.html
Copyright © 2011-2022 走看看