基于ASP.NETAJAX的WebPart开发与部署-转

  • 本文中的信息都是收集来的,来源很多,无法一一列出,望见谅。内容仅作为个人的知识管理。 WindowsSharePointServicesv3基于ASP.NET2.0构建。MicrosoftASP.NETAJAX1.0在MOSS之后推出,因此在某些情况下,ASP.NETAJAX和SharePoint之间存在一些兼容性问题,这些问题将会在
  •  
    为MicrosoftASP.NETAJAX1.0扩展SharePointweb.config
    我们需要为Ajax注册一些特定的条目。编辑SharePoint的web.config文件,该文件通常位于如下目录:

     


    c:\inetpub\wwwroot\wss\virtualdirectories\80
     添加<sectionGroup>元素到 <configSections>标记:<configSections>
          <sectionGroupname="system.web.extensions"type="System.Web.Configuration.SystemWebExtensionsSectionGroup,System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35">
         <sectionGroupname="scripting"type="System.Web.Configuration.ScriptingSectionGroup,System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35">
             <sectionname="scriptResourceHandler"type="System.Web.Configuration.ScriptingScriptResourceHandlerSection,System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"requirePermission="false"allowDefinition="MachineToApplication"/>
           <sectionGroupname="webServices"type="System.Web.Configuration.ScriptingWebServicesSectionGroup,System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35">
             <sectionname="jsonSerialization"type="System.Web.Configuration.ScriptingJsonSerializationSection,System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"requirePermission="false"allowDefinition="Everywhere"/>
             <sectionname="profileService"type="System.Web.Configuration.ScriptingProfileServiceSection,System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"requirePermission="false"allowDefinition="MachineToApplication"/>
             <sectionname="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>
    添加<controls>节的内容,放在<system.web>/<pages>标记中。    <pages>
         <controls>
           <addtagPrefix="asp"namespace="System.Web.UI"assembly="System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"/>
         </controls>
       </pages>   
    在<compilation>标记内的<assemblies>标记中添加下面的内容:      <assemblies>
          <addassembly="System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"/>
         </assemblies>
    在<httpHandlers>节中添加下面的内容: <httpHandlers>
         <addverb="*"path="*.asmx"validate="false"type="System.Web.Script.Services.ScriptHandlerFactory,System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"/>
         <addverb="*"path="*_AppService.axd"validate="false"type="System.Web.Script.Services.ScriptHandlerFactory,System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"/>
         <addverb="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>
    在HttpModules节中添加下面的注册内容,放在所有已有的注册内容下面  <httpModules>
         <addname="ScriptModule"type="System.Web.Handlers.ScriptModule,System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"/>
     </httpModules>
    在<SharePoint>/<SafeControls>节中,添加一条SafeControl,用于MicrosoftAjaxExtensions的System.Web.UI命名空间。  <SafeControls>
         <SafeControlAssembly="System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"Namespace="System.Web.UI"TypeName="*"Safe="True"/>
     </SafeControls>
    最后,添加下面的configuration标记到web.config文件末尾,在结束标记<configuration>前面。
     <system.web.extensions>
       <scripting>
         <webServices>
         <!--Uncommentthislinetoenabletheauthenticationservice.IncluderequireSSL="true"ifappropriate.-->
         <!--
           <authenticationServiceenabled="true"requireSSL="true|false"/>
         -->
         <!--Uncommenttheselinestoenabletheprofileservice.ToallowprofilepropertiestoberetrievedandmodifiedinASP.NETAJAXapplications,youneedtoaddeachpropertynametothereadAccessPropertiesandwriteAccessPropertiesattributes.-->
         <!--
         <profileServiceenabled="true"
                         readAccessProperties="propertyname1,propertyname2"
                         writeAccessProperties="propertyname1,propertyname2"/>
         -->
         </webServices>
         <!--
         <scriptResourceHandlerenableCompression="true"enableCaching="true"/>
         -->
       </scripting>
     </system.web.extensions>
     <system.webServer>
       <validationvalidateIntegratedModeConfiguration="false"/>
       <modules>
         <addname="ScriptModule"preCondition="integratedMode"type="System.Web.Handlers.ScriptModule,System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"/>
       </modules>
       <handlers>
         <removename="WebServiceHandlerFactory-Integrated"/>
         <addname="ScriptHandlerFactory"verb="*"path="*.asmx"preCondition="integratedMode"
              type="System.Web.Script.Services.ScriptHandlerFactory,System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"/>
         <addname="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"/>
         <addname="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>
    利用AjaxBaseWebPart进行开发

    编写使用该扩展的WebPart最简单的办法就是直接继承别人写好的AjaxBaseWebPart。下面是我用的一个。您也可以用其他的,或自己写一个。
    下面是命名空间引用部分:
     usingSystem;
    usingSystem.Collections.Generic;
    usingSystem.Text;
    usingSystem.Web;
    usingSystem.Web.UI.WebControls.WebParts;
    usingSystem.Xml.Serialization;
    usingSystem.Web.UI;
    usingMicrosoft.SharePoint.WebPartPages;
    usingMicrosoft.SharePoint.Utilities;
    usingSystem.Web.UI.WebControls;
    usingSystem.Drawing;
    下面是AjaxBaseWebPart类的实现部分:
         ///<summary>
       ///AbaseclassthatimplementsallthefunctionalityrequiredtouseASP.netAjaxextensionsinsideWSS
       ///</summary>
       [XmlRoot(Namespace="Deps.AjaxBaseWebpart")]
       publicabstractclassAjaxBaseWebpart:Microsoft.SharePoint.WebPartPages.WebPart
       {
           /*
            *TheideaandthecodebehindthisbasewebpartwastakenfromEricsblogpostat:
            *
            *ThisbasicallymanagesthepresenceandconfigurationoftheScriptManager
            *whichisrequiredbyASP.netajaxextensionstohandlepostbacks,ect.Thiswebpartalsoincludes
            *acommonmethodforhandlingerrors.
            */
           #regionDeclarations
           privatestring_ValidationGroupId;
           privateValidationSummary_ErrorContainer;
           privateScriptManager_AjaxManager;
           #endregion

           #regionConstructor
           publicAjaxBaseWebpart()
           {

           }
           #endregion

           #regionMethods
           ///<summary>
           ///Usedtoprovideacommonwaytodisplayerrorstotheuserofthecurrentwebpart.
           ///</summary>
           ///<paramname="message">Descriptionoftheerrorthatoccured.</param>
           publicvoidRegisterError(stringmessage)
           {
               if(this.Controls.Contains(_ErrorContainer))
               {
                   //thiswayofgeneratingauniquecontrolidisusedinsomeoftheOOBwebparts
                   intuniqueCounter;
                   if(HttpContext.Current.Items["GetUniqueControlId"]!=null)
                   {
                       uniqueCounter=(int)HttpContext.Current.Items["GetUniqueControlId"];
                   }
                   else
                   {
                       uniqueCounter=0;
                   }
                   uniqueCounter++;
                   HttpContext.Current.Items["GetUniqueControlId"]=uniqueCounter;

                   //createacustomvalidatortoregisterthecurrenterrormessagewiththeValidationSummarycontrol
                   CustomValidatorcv=newCustomValidator();
                   cv.ID=string.Concat("_Error_",uniqueCounter);
                   cv.ValidationGroup=_ValidationGroupId;
                   cv.Display=ValidatorDisplay.None;
                   cv.IsValid=false;
                   cv.ErrorMessage=message;

                   this.Controls.Add(cv);
               }
               else
               {
                   //ifRegisterErroriscalledbeforetheCreateChildControlsoverrideinAjaxBasePartthentransfertheusertoanerrorpageusingtheSPUtility
                   SPUtility.TransferToErrorPage("TheCreateChildControlsfunctionoftheAjaxBaseParthasnotbeencalled. Youprobablyneedtoadd\"base.CreateChildControls()\"tothetopofyourCreateChildControlsoverride.");
               }
           }
           ///<summary>
           ///NeedstobecalledtoensurethattheValidationSummarycontrolisregisteredonthepage. Anychildwebpartswillneedtohavebase.CreateChildControls()atthetopoftheirownCreateChildControlsoverride.
           ///</summary>
           protectedoverridevoidCreateChildControls()
           {
               base.CreateChildControls();

               if(!this.Controls.Contains(_ErrorContainer))
               {
                   _ValidationGroupId=Guid.NewGuid().ToString();

                   _ErrorContainer=newValidationSummary();
                   _ErrorContainer.ID="_ErrorContainer";
                   _ErrorContainer.ValidationGroup=_ValidationGroupId;
                   _ErrorContainer.BorderStyle=BorderStyle.Solid;
                   _ErrorContainer.BorderWidth=Unit.Pixel(3);
                   _ErrorContainer.BorderColor=Color.Red;

                   this.Controls.Add(_ErrorContainer);
               }
           }
           #endregion

           #regionEvents
           ///<summary>
           ///Oninitfiresbeforepageload.ModificationstothepagethatarenecessarytosupportAjaxaredonehere.
           ///</summary>
           protectedoverridevoidOnInit(EventArgse)
           {
               base.OnInit(e);

               //gettheexistingScriptManagerifitexistsonthepage
               _AjaxManager=ScriptManager.GetCurrent(this.Page);

               if(_AjaxManager==null)
               {
                   //createnewScriptManagerandEnablePartialRendering
                   _AjaxManager=newScriptManager();
                   _AjaxManager.EnablePartialRendering=true;

                   //Fixproblemwithpostbacksandformactions(DevDiv55525)
                   Page.ClientScript.RegisterStartupScript(typeof(AjaxBaseWebpart),this.ID,"_spOriginalFormAction=document.forms[0].action;",true);

                   //tag:"form"att:"onsubmit"val:"return_spFormOnSubmitWrapper()"blocksasyncpostbacksafterthefirstone
                   //notcalling"_spFormOnSubmitWrapper()"breaksallpostbacks


 //returningtrueallthetime,somewhatdefeatsthepurposeofthe_spFormOnSubmitWrapper()whichistoblockrepetitivepostbacks,butitallowsMSAJAXExtensionstoworkproperly
               //itsahackthathopefullyhasminimaleffect
               if(this.Page.Form!=null)
               {
                   stringformOnSubmitAtt=this.Page.Form.Attributes["onsubmit"];
                   if(!string.IsNullOrEmpty(formOnSubmitAtt)&&formOnSubmitAtt=="return_spFormOnSubmitWrapper();")
                   {
                       this.Page.Form.Attributes["onsubmit"]="_spFormOnSubmitWrapper();";
                   }

 

                   //addtheScriptManagerasthefirstcontrolinthePage.Form
                   //Idon'tthinkthisactuallymatters,butIdidittobeconsistentwithhowyouaresupposedtoplacetheScriptManagerwhenuseddeclaritevly
                   this.Page.Form.Controls.AddAt(0,_AjaxManager);
               }
           }
       }
       #endregion

       #regionProperties
       ///<summary>
       ///ExposesthePage'sscriptmanager.ThevalueisnotsetuntilafterOnInit
       ///</summary>
       [WebPartStorage(Storage.None)]
       publicScriptManagerAjaxManager
       {
           get{return_AjaxManager;}
           set{_AjaxManager=value;}
       }
       #endregion
   }
开发时只要继承这个WebPart就可以添加UpdatePanel,并在里面添加其他控件了。

转载于:https://www.cnblogs.com/dnmidi/archive/2007/10/21/932417.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值