ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-07-15 12:04:04  阅读:184  来源: 互联网

标签:Web WebPart ASP usingSystem NETAJAX ErrorContainer 添加 AjaxManager cv


原文链接:http://www.cnblogs.com/dnmidi/archive/2007/10/21/932417.html
  • 本文中的信息都是收集来的,来源很多,无法一一列出,望见谅。内容仅作为个人的知识管理。 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

标签:Web,WebPart,ASP,usingSystem,NETAJAX,ErrorContainer,添加,AjaxManager,cv
来源: https://blog.csdn.net/weixin_30814329/article/details/95949424

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有