Monday, January 25, 2010

01/25

 

SQLHelper Timeout Issue:

 

Can add commandTimeout with seconds

// Create a command and prepare it for execution
           SqlCommand cmd = new SqlCommand();

           // Add by Dayang

           cmd.CommandTimeout = 300;

 

 

SP

Simple Ajax Enabled Webpart with Easy Deploy

  • Sample Project Name : AjaxEnabledWebPart2
  • Steps :
    • Add new project with Sharepoint->Web part
    • Add System.Web.Extensions( to support ajax)
    • Add a new web part item with following code
    • Set web folder in project.Property->Debug
    • Deploy

using System;

using System.Collections;

using System.Text;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI;

using System.Runtime.InteropServices;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

namespace AjaxEnabledWebPart2
{
    [Guid("76ab24f0-73ce-45ee-bfaf-947c8e8e3a45")]
    public class AjaxEnabledWebpart2 : System.Web.UI.WebControls.WebParts.WebPart
    {
        private Label label;

        private TextBox textBox;

        private UpdatePanel up;

        private Button button;

        private ScriptManager _AjaxManager;

        //自定义一个ScriptManager用来存储获取的ScriptManager

        [WebPartStorage(Storage.None)]

        public ScriptManager AjaxManager
        {

            get { return _AjaxManager; }

            set { _AjaxManager = value; }

        }

        //页面初始化事件

        protected override void OnInit(EventArgs e)
        {

            base.OnInit(e);

            //获取页面上的ScriptManager

            _AjaxManager = ScriptManager.GetCurrent(this.Page);

            //如果页面上没有ScriptManager时追加一个

            if (_AjaxManager == null)
            {

                _AjaxManager = new ScriptManager();

                _AjaxManager.EnablePartialRendering = true;

                Page.ClientScript.RegisterStartupScript(

                    typeof(AjaxEnabledWebpart2),

                    this.ID,

                    "_spOriginalFormAction = document.forms[0].action;",

                    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, _AjaxManager);

                }

            }

        }

        //创建各个控件,并填充UpdatePanel

        protected override void CreateChildControls()
        {

            base.CreateChildControls();

            this.EnsureUpdatePanelFixups();

            up = new UpdatePanel();

            up.ID = "UpdatePanel1";

            up.ChildrenAsTriggers = true;

            up.RenderMode = UpdatePanelRenderMode.Inline;

            up.UpdateMode = UpdatePanelUpdateMode.Always;

            this.textBox = new TextBox();

            this.textBox.ID = "TextBox";

            up.ContentTemplateContainer.Controls.Add(this.textBox);

            this.label = new Label();

            this.label.Text = "Enter your name.";

            up.ContentTemplateContainer.Controls.Add(this.label);

            this.button = new Button();

            button.CausesValidation = false;

            button.ID = "Button1";

            button.Text = "Say Hello";

            button.Click += new EventHandler(HandleButtonClick);

            up.ContentTemplateContainer.Controls.Add(this.button);

            _AjaxManager.RegisterAsyncPostBackControl(this.button);

            this.Controls.Add(up);

        }

        private void HandleButtonClick(object sender, EventArgs eventArgs)
        {

            this.label.Text = "Hello " + this.textBox.Text;

        }

        /*在MOSS中,为了正确解析某些特殊的URLs,例如包含汉字等两比特字符的URL,

         * Windows SharePoint Services JavaScript使用form onSubmit wrapper重载默认的form action,

         * 下面这个方法用来恢复默认的form action,如果你的URL里不包含汉字等双字节的字符,

         * 那么,恭喜,你可以使用ASP.NET AJAX UpdatePanels

         * (具体请参考上文中提到的「moss开发团队blog」) */

        private void EnsureUpdatePanelFixups()
        {

            if (this.Page.Form != null)
            {

                string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];

                if (formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
                {

                    this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";

                }

            }

            ScriptManager.RegisterStartupScript(

                this,

                typeof(AjaxEnabledWebpart2),

                "UpdatePanelFixup",

                "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;",

                true);

        }

    }
}

0 Comments:

Post a Comment

<< Home