JOJ
踏踏实实做人,认认真真做事!放纵自己就是毁灭自己!

代码来源于网络,新建类库项目,添加类UserAjaxTest!注意把项目强签名!否则部署会部署到sharepoint会不成功! 下载WSPBuilder工具,生成WSP! 部署到sharepoint即可!

一下代码我测试通过了!

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using System.Web.UI.WebControls;

///添加引用 "System.Web.Extensions" 注意版本选择 应和你的 .net 版本相同
///添加引用 "System.web"
namespace Sample.SayHello
{
    public class UserAjaxTest : WebPart
    {
        private Label displayName;
        private TextBox inputName;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            //Fix for the UpdatePanel postback behaviour.
            EnsurePanelFix();

            LinkButton sayHello = new LinkButton();
            UpdatePanel refreshName = new UpdatePanel();
            ScriptManager scriptHandler = new ScriptManager();
            displayName = new Label();
            inputName = new TextBox();

            //Set up control properties.
            this.displayName.ID = "displayName";
            this.displayName.Text = "Hello!";
            this.inputName.ID = "inputName";
            sayHello.ID = "sayHello";
            sayHello.Text = "Say Hello";
            scriptHandler.ID = "scriptHandler";
            refreshName.ID = "refreshName";
            refreshName.UpdateMode = UpdatePanelUpdateMode.Conditional;
            refreshName.ChildrenAsTriggers = true;

            //Add the EventHandler to the Button.
            sayHello.Click += new EventHandler(ClickHandler);

            //Add the user interface (UI) controls to the UpdatePanel.
            refreshName.ContentTemplateContainer.Controls.Add(this.inputName);
            refreshName.ContentTemplateContainer.Controls.Add(sayHello);
            refreshName.ContentTemplateContainer.Controls.Add(this.displayName);

            //The ScriptManager control must be added first.
            this.Controls.Add(scriptHandler);
            this.Controls.Add(refreshName);
        }

        private void ClickHandler(object sender, EventArgs args)
        {
            this.displayName.Text = "Hello " + this.inputName.Text.ToString() + " !";
        }

        private void EnsurePanelFix()
        {
            if (this.Page.Form != null)
            {
                String fixupScript = @"
     _spBodyOnLoadFunctionNames.push(""_initFormActionAjax"");
     function _initFormActionAjax
     {
       if (_spEscapedFormAction == document.forms[0].action)
       {
         document.forms[0]._initialAction = 
         document.forms[0].action;
       }
     }
     var RestoreToOriginalFormActionCore = 
       RestoreToOriginalFormAction;
     RestoreToOriginalFormAction = function
     {
       if (_spOriginalFormAction != null)
       {
         RestoreToOriginalFormActionCore;
         document.forms[0]._initialAction = 
         document.forms[0].action;
       }
     }";
                ScriptManager.RegisterStartupScript(this,
                  typeof(UserAjaxTest), "UpdatePanelFixup",
                  fixupScript, true);
            }
        }
    }
}
posted on 2010-04-16 18:37  JoinJ  阅读(300)  评论(0编辑  收藏  举报