MOSS,AJAX運用

來源:互聯網
上載者:User

在MOSS裡面運用AJAX,現在大家也弄的很火,我也來說2句,現在MOSS支援的AJAX不是很好,聽說MOSS的SP1出來以後,會完全支援AJAX,那個時候的MOSS大家想一想。。。。。
下面介紹一下,在MOSS裡面簡單運用AJAX,下面這個列子是我在我的做的一個DEMO,
希望對學習 MOSS AJAX的有些協助。。。
個人還有一個觀點。有時候,代碼看不懂不要緊,首先把代碼現跑起來 。運用起來,慢慢的就理解拉。。。。
這個是MOSS的設定檔, 要運用AJAX,需要的設定檔
c:\inetpub\wwwroot\wss\virtualdirectories\80
  1.添加 <sectionGroup>元素到  <configSections>標記: <configSections>
       <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
      <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
          <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
        <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
          <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere" />
          <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
          <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
        </sectionGroup>
      </sectionGroup>
    </sectionGroup>
</configSections>
2.添加 <controls> 節的內容,放在 <system.web>/<pages> 標記中。     <pages>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </controls>
    </pages>   
3.在<compilation>標記內的<assemblies> 標記中添加下面的內容:       <assemblies>
       <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </assemblies>
4.在 <httpHandlers> 節中添加下面的內容:  <httpHandlers>
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
  </httpHandlers>
5.在 HttpModules 節中添加下面的註冊內容,放在所有已有的註冊內容下面   <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </httpModules>
6.在<SharePoint>/<SafeControls>節中,添加一條 SafeControl ,用於 Microsoft Ajax Extensions的System.Web.UI命名空間。   <SafeControls>
      <SafeControl Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TypeName="*" Safe="True" />
  </SafeControls>
7.最後,添加下面的 configuration標記到 web.config檔案末尾, 在結束標記<configuration>前面。
  <system.web.extensions>
    <scripting>
      <webServices>
      <!-- Uncomment this line to enable the authentication service. Include requireSSL="true" if appropriate. -->
      <!--
        <authenticationService enabled="true" requireSSL = "true|false"/>
      -->
      <!-- Uncomment these lines to enable the profile service. To allow profile properties to be retrieved and modified in ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and writeAccessProperties attributes. -->
      <!--
      <profileService enabled="true"
                      readAccessProperties="propertyname1,propertyname2"
                      writeAccessProperties="propertyname1,propertyname2" />
      -->
      </webServices>
      <!--
      <scriptResourceHandler enableCompression="true" enableCaching="true" />
      -->
    </scripting>
  </system.web.extensions>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules>
      <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    </modules>
    <handlers>
      <remove name="WebServiceHandlerFactory-Integrated" />
      <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode"
           type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </handlers>
  </system.webServer>
設定檔配置好,我們現在就開始開發運用AJAX。。
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Web;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace AjaxShow
{
    [Guid("ca7e29da-ecd8-4151-baea-e75f08d0b9bd")]
    public class AjaxShow : System.Web.UI.WebControls.WebParts.WebPart
    {
        public AjaxShow()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        UpdatePanel UP;
        protected Label LB;
        protected Timer TT;

        //ScriptManager在PreRender時期會去找所有的Client端Script

        //而CreateChildControl() 裡的Control 也是在PreRpender才建立
        //所以如果在CreateChildControl建立的話會出錯

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ScriptManager SM = ScriptManager.GetCurrent(this.Page);
            if (SM == null)
            {
                SM = new ScriptManager();
                SM.ID = "SM";
                if (this.Page.IsPostBack)
                {
                    Page.ClientScript.RegisterStartupScript(typeof(AjaxShow),
                        this.ID, "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;", true);

                }

                if (this.Page.Form != null)
                {

                    string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];

                    if (!string.IsNullOrEmpty(formOnSubmitAtt) && formOnSubmitAtt ==
                    "return _spFormOnSubmitWrapper();")
                    {

                        this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";

                    }

                    this.Page.Form.Controls.AddAt(0, SM);

                }

            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            try
            {
                LB = new Label();
                LB.ID = "LB";
                LB.Text = "AA  Ajax";
                TT = new Timer();
                TT.ID = "TT";
                TT.Enabled = true;
                TT.Interval = 3000;
                TT.Tick += new EventHandler<EventArgs>(TT_Tick);
                UP = new UpdatePanel();
                UP.ID = "UP";
                UP.ContentTemplateContainer.Controls.Add(LB);
                UP.ContentTemplateContainer.Controls.Add(TT);
                //設定UpdatePanelUpda
                UP.UpdateMode = UpdatePanelUpdateMode.Conditional;
                AsyncPostBackTrigger apbk = new AsyncPostBackTrigger();
                apbk.ControlID = "TT";
                apbk.EventName = "Tick";
                UP.Triggers.Add(apbk);
                this.Controls.Add(UP);
            }
            catch (Exception ex)
            {
                Literal errMsg = new Literal();
                errMsg.Text = ex.Message;
                this.Controls.Add(errMsg);
            }
        }

        void TT_Tick(object sender, EventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
            LB.Text = GetQuery();
        }

        protected string GetQuery()
        {
            //SPSite site = SPControl.GetContextSite(System.Web.HttpContext.Current);
            //SPWeb web = site.RootWeb;
            //SPList list = web.Lists["CustomList"];

 

            SPSite site = new SPSite(@"http://lh-vmpc/personal/test");
            SPWeb web = site.OpenWeb();
            SPList list = web.Lists["CustomList"];

            SPListItemCollection lists = list.Items;
            string strResult = "";
            int i = list.ItemCount;
            int ResultRank = 1;
            if (i < 1)
            {
                strResult = "no info.............haha";
            }
            else
            {
                Random r = new Random();
                ResultRank = r.Next(1, i);
                strResult = lists[ResultRank].Name;
            }

            return strResult;

        }
        protected override void Render(HtmlTextWriter writer)
        {
            UP.RenderControl(writer);
        }
    }
}

大家有什麼不明白。。。可以提出。。。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.