zoukankan      html  css  js  c++  java
  • SharePoint UserControl

    SharePoint User Control 的开发有多种,现在主要介绍其中的两种。

    1.  所有的代码都写在后辍名为ascx的文件中,如下图。 其优点为是容易更新到SharePoint网站, 不需要dll 编辑; 缺点:容易代码出错,没有智能检验,不可以跟踪代码:

    <%@ Control Language="C#" AutoEventWireup="true"  ClassName="PosterscopeTopNavigation2" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Configuration" %>
    <%@ Import Namespace="System.Collections" %>
    <%@ Import Namespace="System.Web" %>
    <%@ Import Namespace="System.Web.Security" %>
    <%@ Import Namespace="System.Web.UI" %>
    <%@ Import Namespace="System.Web.UI.WebControls" %>
    <%@ Import Namespace="System.Web.UI.WebControls.WebParts" %>
    <%@ Import Namespace="System.Web.UI.HtmlControls" %>
    <%@ Import Namespace="System.Text" %>
    <%@ Import Namespace="System.Xml" %>
    <%@ Import Namespace="System.IO" %>
    <%@ Import Namespace="Microsoft.SharePoint" %>
    <%@ Import Namespace="Microsoft.SharePoint.Navigation" %>
    
    <script runat="server">
        private string mediaPhotos = "Media Photos";
        private string CategoryImages = "/sites/Posterscopeusa/Pages/CategoryImages.aspx?MCategory={0}&KCategory={1}";
        
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.IsPostBack)
                    Bind();
            }
            catch (Exception ex)
            {
                //this.Response.Write(ex.Message);
            }
        }
        private void Bind()
        {
            this.ddlMedia.Items.Clear();
            this.ddlMarket.Items.Clear();
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb rootWeb = site.RootWeb)
                    {
                        rootWeb.AllowUnsafeUpdates = true;
                        string loginUser = HttpContext.Current.User.Identity.Name;
                        SortedList MSlist = new SortedList();
                        SortedList KSlist = new SortedList();
                        foreach (SPWeb web in site.AllWebs)
                        {
                            try
                            {
                                if (!web.ID.Equals(site.RootWeb.ID))
                                {
                                    SPUser user = site.RootWeb.EnsureUser(loginUser);
                                    if (web.DoesUserHavePermissions(user.LoginName, SPBasePermissions.ViewListItems))
                                    {
                                        SPList list = null;
                                        try
                                        {
                                            list = web.Lists[mediaPhotos];
                                        }
                                        catch { }
                                        if (list != null)
                                        {
                                            MSlist.Clear();
                                            KSlist.Clear();
                                            //Fill media category and market category column choices to MSlist and KSlist SortedList
                                            SPFieldChoice MediaCategoryField = (SPFieldChoice)list.Fields.GetField("Media Category");
                                            foreach (string choiceName in MediaCategoryField.Choices)
                                            {
                                                string name = Server.HtmlDecode("   ") + choiceName;
                                                //ListItem item = new ListItem(name, string.Format(MCategoryImages, choiceName));
                                                ListItem item = new ListItem(name, choiceName);
                                                MSlist.Add(name, item);
                                            }
                                            SPFieldChoice MarketCategoryField = (SPFieldChoice)list.Fields.GetField("Market Category");
                                            foreach (string choiceName in MarketCategoryField.Choices)
                                            {
                                                string name = Server.HtmlDecode("   ") + choiceName;
                                               // ListItem item = new ListItem(name, string.Format(KCategoryImages, choiceName));
                                                ListItem item = new ListItem(name,choiceName);
                                                KSlist.Add(name, item);
                                            }
                                            break;
                                        }
                                    }
                                }
                            }
                            catch
                            { }
                            finally
                            {
                                if (web != null)
                                {
                                    web.Dispose();
                                }
                            }
    
                        }
                        
                        rootWeb.AllowUnsafeUpdates = false;
    
                        string MTitle = "Media Category";
                        string KTitle = "Market Category";
                        //Fill Media Category Dropdown Items
                        ddlMedia.Items.Add(new ListItem("----" + Server.HtmlDecode(" ") + MTitle + "----", ""));
                        if (MSlist.Count > 0)
                        {
                            foreach (ListItem childItem in MSlist.Values)
                            {
                                ddlMedia.Items.Add(childItem);
                            }
                        }
                        //Fill Market Category Dropdown Items
                        ddlMarket.Items.Add(new ListItem("----" + Server.HtmlDecode(" ") + KTitle + "----", ""));
                        if (KSlist.Count > 0)
                        {
                            foreach (ListItem childItem in KSlist.Values)
                            {
                                ddlMarket.Items.Add(childItem);
                            }
                        }
                        if (Request.QueryString["MCategory"] != null)
                        {
                            string mcategoryStr = Request.QueryString["MCategory"].ToString();
                            if (!string.IsNullOrEmpty(mcategoryStr))
                            {
                                //MTitle = string.Format("Media Category: {0}", mcategoryStr);
                                ddlMedia.Items.Insert(0, new ListItem(Server.HtmlDecode(" ") + MTitle, ""));
                                ddlMedia.SelectedValue = mcategoryStr;
                                //Page.RegisterStartupScript("Message", "<script>alert('"+MTitle+"');</sc"+"ript>");
                             }
                            else
                            {
                                ddlMedia.SelectedIndex = 0;
                            }
                        }
    
                        if (Request.QueryString["KCategory"] != null)
                        {
                            string kcategorystr = Request.QueryString["KCategory"].ToString();
                            if (!string.IsNullOrEmpty(kcategorystr))
                            {
                                //KTitle = string.Format("Market Category: {0}", kcategorystr);
                                ddlMarket.Items.Insert(0, new ListItem(Server.HtmlDecode(" ") + KTitle, ""));
                                ddlMarket.SelectedValue = kcategorystr;
                            }
                            else
                            {
                                ddlMarket.SelectedIndex = 0;
                            }
                        }
                    }
                }
            });
        }
    
       
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.Response.Redirect(string.Format(CategoryImages,ddlMedia.SelectedValue.ToString(),ddlMarket.SelectedValue.ToString()));
        }
        
    </script>
    <style type="text/css">
    .btn {float:right;
    margin-top:5px;
    BORDER-RIGHT: #7b9ebd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #7b9ebd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 12px; FILTER: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#ffffff, EndColorStr=#cecfde); BORDER-LEFT: #7b9ebd 1px solid; CURSOR: hand; COLOR: black; PADDING-TOP: 2px; BORDER-BOTTOM: #7b9ebd 1px solid
    }
    </style>
    <div>
    <!--Media Category Dropdown-->
    <asp:DropDownList ID="ddlMedia" runat="server" AutoPostBack="False"  
    style="background:#dce5e4; color:blue; text-decoration: none;border: 1px inset silver;margin-top:5px;230px;" >
    </asp:DropDownList></div>
    <!--Market Category Dropdown-->
    <div><asp:DropDownList ID="ddlMarket" runat="server" AutoPostBack="False" 
    style="background:#dce5e4; color:blue; text-decoration: none;border: 1px inset silver;margin-top:5px; 230px;" >
    </asp:DropDownList></div>
    <!--Search button-->
    <div><asp:Button ID="btnSearch" runat="server" Text="Search" CssClass="btn" OnClick="btnSearch_Click"/> </div>
    
    
     

    2. 第二种方法,所有的UI代码写在后辍名为ascx的文件中,其后台代码在项目中new一个新类中。其优点:可跟踪代码,写代码不容易出错;缺点:需要编辑和更新dll文件。

    QueryControls.cs

    <%@ Control Language="C#" AutoEventWireup="true" Inherits="VCSharePoint.BL.QueryControls,VCSharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0134fd28ed40d3b2" %>
    <style type="text/css">
            .labeltitle{font-family:Calibri; font-size:10pt;font-weight:bold;	color:#666;}
            .select{ font-family:Calibri; font-size:10pt; 160px;   margin-left:10px}
    	</style>
    <table style="700px">
        <tr>
            <td style="70px">
                <input id="relWebURL" type="hidden" runat="server" /></td>
            <td style=" 100px;"   align="right" >
                <asp:Label ID="lbYear" runat="server"  CssClass="labeltitle" Text="Year:"></asp:Label>
            </td>
            <td>
                <asp:DropDownList ID="ddlYears" runat="server" CssClass="select"   AutoPostBack="True" OnSelectedIndexChanged="ddlYears_SelectedIndexChanged">
                </asp:DropDownList>
            </td>
            <td style=" 100px;"   align="right" >
                <asp:Label ID="lbMonth" runat="server" Text="Month:" CssClass="labeltitle"></asp:Label>
            </td>
            <td>
                <asp:DropDownList ID="ddlMonths" runat="server" CssClass="select"> </asp:DropDownList>
            </td>
        </tr>
    </table>
    

    QueryControls.cs文件代码:

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;
    namespace VCSharePoint.BL
    {
        public partial class QueryControls : System.Web.UI.UserControl
        {
            public DropDownList ddlYears;
            public DropDownList ddlMonths;
            public HtmlInputHidden relWebURL;
            /// <summary>
            ///  the page inital function
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.Page.IsPostBack)
                {
                    BLCalculate blcalc = new BLCalculate();
                    DataTable ymTable = blcalc.GetYMData();
    
                    foreach (DataRow row in ymTable.Rows)
                    {
                        object oYear = row[UNITS.YMfields[0]]; // get the year value
    
                        if (oYear != null && (!string.IsNullOrEmpty(oYear.ToString())))
                        {
                            ListItem y = new ListItem();
                            y.Text = oYear.ToString();
                            y.Value = oYear.ToString();
    
                            if (oYear.ToString() == UNITS.CurrYear) // Default is setting current year
                            {
                                y.Selected = true;
    
                                object oMonth = row[UNITS.YMfields[1]]; // get the month value
                                if (oMonth != null && (!string.IsNullOrEmpty(oMonth.ToString())))
                                {
                                    ListItem m = new ListItem(oMonth.ToString(), oMonth.ToString());
    
                                    if (!ddlMonths.Items.Contains(m))
                                    {
                                        ddlMonths.Items.Add(m); // set the value into month dropdown
                                    }
                                }
                            }
    
                            if (!ddlYears.Items.Contains(y)) // Set the values into year dropdown
                                ddlYears.Items.Add(y);
                        }
                    }
    
                    if (ddlYears.Items.Count == 0)
                        ddlYears.Items.Add(new ListItem(UNITS.CurrYear, UNITS.CurrYear)); // set a current year into year dropdown if there is no data in voluteer list.
    
                    ListItem mTotal = new ListItem("Total", "All"); // set a total option in month
                    mTotal.Selected = true;
                    ddlMonths.Items.Insert(0, mTotal);
                }
    
                relWebURL.Value = SPContext.Current.Web.ServerRelativeUrl; // get Relative Web URL
            }
    
    
            /// <summary>
            ///  Year dropdown select eventhandler
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void ddlYears_SelectedIndexChanged(object sender, EventArgs e)
            {
                string sYear = ddlYears.SelectedItem.Value;
    
                if (sYear != null)
                {
    
                    BLCalculate blcalc = new BLCalculate();
                    DataTable ymTable = blcalc.GetYMData();
    
                    ddlMonths.Items.Clear();
    
                    foreach (DataRow row in ymTable.Rows)
                    {
                        object oYear = row[UNITS.YMfields[0]]; // get the year value
    
                        if (oYear != null && (!string.IsNullOrEmpty(oYear.ToString())))
                        {
                            if (oYear.ToString() == sYear) // get the month based on selecting year.
                            {
                                object oMonth = row[UNITS.YMfields[1]]; // get the month value
    
                                if (oMonth != null && (!string.IsNullOrEmpty(oMonth.ToString())))
                                {
                                    ListItem m = new ListItem(oMonth.ToString(), oMonth.ToString());
    
                                    if (!ddlMonths.Items.Contains(m))
                                    {
                                        ddlMonths.Items.Add(m); // set the value into month dropdown
                                    }
                                }
                            }
                        }
                    }
                    ListItem mTotal = new ListItem("Total", "All"); // set a total option in month
                    mTotal.Selected = true;
                    ddlMonths.Items.Insert(0, mTotal);
               }
            }
        }
    }
    

    其他待续……

  • 相关阅读:
    悟透JavaScript(理解JS面向对象的好文章)
    ClassLoader的等级加载机制
    ClassLoader的类结构分析
    如何实现自己的ClassLoader
    Servlet的ClassLoader
    idea远程调试linux下的tomcat
    centos VM 识别U盘
    linux yum 命令
    centos 基本操作(快捷键开户终端,复制,粘贴,yum命令)
    CentOS 配置Apache+Mysql+PHP (yum)与卸载
  • 原文地址:https://www.cnblogs.com/gzh4455/p/2238857.html
Copyright © 2011-2022 走看看