`
ruilinruirui
  • 浏览: 1049037 次
文章分类
社区版块
存档分类
最新评论

Create a AJAX enabled WebPart for SharePoint2007 by using ASP.NET 2.0 client callback feature

 
阅读更多
Create a AJAX enabled WebPart for SharePoint2007 by using ASP.NET 2.0 client callback feature

I tried to integrate Atlas into WebPart a couple of days ago but failed. Atlas is a great AJAX framework from Microsoft, but it isn't compatible with SharePoint 2007 for now, what a pity.

But your customers always have this kind of complain, "The page will fully reload when I just click one button in the WebPart, I just wanna perform a very simple action but I have to wait until the page load complete." So, how can you handle this?

As you've known, there is a built-in feature called client callback in ASP.NET 2.0, which allows you call server method from client side without causing the page refresh, and SharePoint2007 is built upon ASP.NET 2.0 framework, so we can use this feature to support AJAX in WebPart.

In the following steps, I'm gonna show you how to get server date from client in a WebPart.
[If you are not familiar with Client Callback feature in ASP.NET 2.0 yet, I strongly recommend you read this article first]

1. Start up VS.NET 2005 and create a Class Library project.

2. Add System.Web.dll to the project reference.

3. Create a Class with a name you expect, which inherit from System.Web.UI.WebControls.WebParts.WebPart and implement System.Web.UI.ICallbackEventHandler interface.

4. Override CreateChildControls to create some controls.

5.Override OnLoad to regist related JavaScript to client.

6. Override RenderContents or Render to render WebPart to browser.

7.Implement ICallbackEventHandler members.

The source code is here:

usingSystem;
usingSystem.Text;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls.WebParts;

namespaceSPS2007.Research.WebParts
{
publicclassAjaxPart:WebPart,ICallbackEventHandler
{
privatestringstrCallResult;
privateHtmlInputTexttbResult;
privateHtmlButtonbtnCall;

publicAjaxPart()
{

}


protectedoverridevoidCreateChildControls()
{
base.CreateChildControls();

tbResult
=newHtmlInputText();
tbResult.ID
="myTextBox";
btnCall
=newHtmlButton();
btnCall.InnerText
="GetServerDate";
btnCall.Attributes.Add(
"OnClick","javascript:InvokeMethod();");
this.Controls.Add(tbResult);
this.Controls.Add(btnCall);
}


protectedoverridevoidOnLoad(EventArgse)
{
if((this.Page!=null)&&!this.Page.IsCallback)
{
ClientScriptManagerscriptManager
=this.Page.ClientScript;

stringeventRef=scriptManager.GetCallbackEventReference(this,"argVal","OnCallComplete","ctxVal","OnErrorOccurs",true);

StringBuildersb
=newStringBuilder();

sb.Append(
"functionInvokeMethod(argVal,ctxVal)");
sb.Append(Environment.NewLine);
sb.Append(
"{");
sb.Append(Environment.NewLine);
sb.Append(eventRef);
sb.Append(Environment.NewLine);
sb.Append(
"}");
sb.Append(Environment.NewLine);

HtmlInputTexttextBox
=this.FindControl("myTextBox")asHtmlInputText;

sb.Append(
"functionOnCallComplete(result,context)");
sb.Append(Environment.NewLine);
sb.Append(
"{");
sb.Append(Environment.NewLine);
sb.Append(
"document.getElementById(/""+textBox.ClientID+"/").value=result;");
sb.Append(Environment.NewLine);
sb.Append(
"}");
sb.Append(Environment.NewLine);

sb.Append(
"functionOnErrorOccurs(err)");
sb.Append(Environment.NewLine);
sb.Append(
"{");
sb.Append(Environment.NewLine);
sb.Append(
"window.status=err;");
sb.Append(Environment.NewLine);
sb.Append(
"}");
sb.Append(Environment.NewLine);

scriptManager.RegisterClientScriptBlock(
base.GetType(),"CallBackScript",sb.ToString(),true);
base.OnLoad(e);
}

}


protectedoverridevoidRenderContents(HtmlTextWriterwriter)
{
EnsureChildControls();
tbResult.RenderControl(writer);
btnCall.RenderControl(writer);
}


protectedstringGetDateTime()
{
returnDateTime.Now.ToString("MM/dd/yyyy");
}


ImplementICallbackEventHandlerMembers#regionImplementICallbackEventHandlerMembers

publicstringGetCallbackResult()
{
returnstrCallResult;
}


publicvoidRaiseCallbackEvent(stringeventArgument)
{
strCallResult
=GetDateTime();
}


#endregion

}

}

Okay, That's it, you can compile the project and deploy your WebPart to sharepoint site. I will give you the instructions about how to deploy a web part if you don't know that.

1. Copy compiled assembly to C:/Inetpub/wwwroot/wss/VirtualDirectories/80/_app_bin, notice that 80 is the virtual directory related to your wss site, you can find the name from IIS MMC.

2. Modify the web.config file under C:/Inetpub/wwwroot/wss/VirtualDirectories/80 directory, add below line to SafeControls section:

<SafeControl Assembly="SPS2007.Research.WebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" Namespace="SPS2007.Research.WebParts" TypeName="*" />

You probably need to change Assembly and Namespace to accord with your assembly.

3. Execute IISRESET under command line.

4. Then go to Site Settings -> Web parts, click 【New】go to the New Web Parts page, find your web part, click 【Populate Gallery】, after that you can add your web part to a page when you edit a page.

原文:http://fengzhimei.cnblogs.com/archive/2006/05/26/410032.html

分享到:
评论

相关推荐

    asp.net 2.0 webpart in action

    asp.net 2.0 webpart in actionasp.net 2.0 webpart in actionasp.net 2.0 webpart in action

    ASP.NET 2.0+SQL Server 2005全程指南-源代码

    ASP.NET 2.0+SQL Server 2005全程指南 目录 基础篇 第1章 ASP.NET概述及环境配置 1.1 认识ASRNET 1.1.1 .NET Framework框架 1.1.2 ASP.NET功能与特性 1.1.3 ASP.NET与ASP的区别 1.2 搭建ASP.NET开发环境 1.2.1...

    ASP.NET2.0WebPart编程入门(理论篇)

    火龙果软件工程技术中心一、引言 WebPart是ASP.NET2.0中一个非常令人激动的特性。它为创建动态的网页接口提供了一系列的可用控件,使得用户很容易地进行配置或者个性化页面。并且,用户可以象在桌面应用中一样自由...

    asp.net知识库

    深度解析Asp.Net2.0中的Callback机制 使用 Web 标准生成 ASP.NET 2.0 Web 站点 ASP.NET 2.0基于SQLSERVER 2005的aspnetdb.mdf部署 ASP.NET 2.0 Security FAQs Asp.net 2.0功能体验,细节之Web控件(一) 隐藏控件 ...

    ASP.NET与AJAX深度剖析范例集_卷2(源代码)

    ASP.NET与AJAX深度剖析范例集 的随书代码 书的目录如下: 目录 第1章 构建自己的网站 第2章 Visual Studio 2005的重要改变 第3章 ASP.NET 2.0新功能剖析 第4章 数据库访问机制的重大变革 第5章...

    ASP.NET MVC框架下的Webpart插件开发

    ASP.NET MVC框架下的Webpart Portlet插件开发

    ASP.NET中webPart例程(源码)

    (1)关闭webPart后重新打开webPart (2)webPart模式切换 (3)添加新的WebPart组件 供初学者使用,基本包含了webPart的使用

    SharePoint AJAX webpart

    SharePoint.Ajax.Library. 一个功能很强大的,自定义的webpart

    如何使用用户控件 - ASP_NET2_0 -

    moss 2007 开发 如何使用用户控件来开发webpart

    asp.net高级教程 PPT

    第一章 ASP.NET2.0的安全和角色管理.ppt 第二章 缓存技术.ppt 第三章 自定义控件.ppt 第四章 个性化用户配置.ppt 第五章 WebPart.ppt 设计模式.ppt

    Asp.net2005 WebPart 的示例.rar

    Asp.net2005 WebPart示例简单通俗易懂哦。

    Visual C# 2005编程实例精粹 源码

    全书摒弃语法说教,通篇贯穿实例,主要内容包括:使用ADO.NET 2.0访问数据库技巧、使用ASP.NET 2.0设计网站技巧、使用GDI+开发图形文字特效技巧、使用水晶报表专家创建报表技巧及VSTO 2005新技术等。在本书中,还...

    ASP. NET与AJAX深度剖析范例集_卷1(共2卷)(源代码)

    ASP.NET与AJAX深度剖析范例集 的随书代码 书的目录如下: 目录 第1章 构建自己的网站 第2章 Visual Studio 2005的重要改变 第3章 ASP.NET 2.0新功能剖析 第4章 数据库访问机制的重大变革 第5章...

    Sharepoint Webpart开发指导

    本文描述了针对Microsoft Office SharePoint Server 2007的WebPart的开发环境的搭建。开发环境使用的操作系统为Windows 2003 SP1以上,并安装了Windows SharePoint Service 3.0。

    有关SharePoint WebPart的一些文档笔记

    有关SharePoint WebPart的一些文档笔记有关SharePoint WebPart的一些文档笔记有关SharePoint WebPart的一些文档笔记

    WebPart Manager 2.0

    WebPartManager is a win-form tool used for deploy web parts to sharepoint (2007). WebPart部署工具,针对SharePoint 2007设计编写。 You don't need to write the dwp/webpart file or manifest file or web....

    sharepoint webpart 2

    sharepoint webpart sharepoint webpartsharepoint webpart

    sharepoint webpart 开发 SmartPart

    用于sharepoint webpart 的开发,用此软件之后直接开发usercontrol即可。和国内的quickpart 差不多

    sharepoint webpart 4

    sharepoint webpart4 sharepoint webpart

Global site tag (gtag.js) - Google Analytics