承接MOSS各种工作流开发 联系人:王先生.电话:18618405729QQ:252385878 QQ群:41696750 MSN:wanghao-3@hotmail.com

导航

MOSS,AJAX运用

在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);
        }
    }
}

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

posted on 2007-06-21 09:57  A A  阅读(3185)  评论(12编辑  收藏  举报