상세 컨텐츠

본문 제목

[MOSS] Ajax Webpart HelloWorld

MOSS

by happynuri 2008. 9. 10. 16:25

본문

네... 오늘도 피곤한 하루가 지나가고 있군요.. ㅎㅎ

msdn에서 sharepoint 웹파트 개발시 이렇게 만들어라!! 라는 것을 그대로 따라 하다보면은

버튼 클릭시 텍스트 박스의 글씨가 레이블에 그대로 옮겨져야 하는 코드 인데...

어?? 이상하다 처음 한번은 ajax가 잘 되는데 두번 째 부터는 반응이 없네??

라는 생각을 하실수가 있으십니다.

저는 삽질 많이 했습죠~

그니까 ajax를 구현 하려면

기본적인 web.config 셋팅이 선행되어야 할것이고... default.master 등에 scriptManager 등은

추가 되어 있어야 할것입니다.

그럼 바로 예제 코드 나갑니다.

using System.Web.Extensions 는 추가 하셔야 합니다.


    public class AjaxWP1 : System.Web.UI.WebControls.WebParts.WebPart
    {

        private Label displayName;
        private TextBox inputName;

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

            LinkButton sayHello = new LinkButton();
            sayHello.Text = "버튼";
            UpdatePanel refreshName = new UpdatePanel();
           // ScriptManager scriptHandler = new ScriptManager(); //저는 마스터페이지에 추가 해 두어서 주석 합니다.
            displayName = new Label();
            inputName = new TextBox();

            this.displayName.ID = "displayName";
            this.displayName.Text = "Hello";
            this.inputName.ID = "inputName";
            sayHello.ID = "sayhello";
           // scriptHandler.ID = "scriptHandler";
            refreshName.ID = "refreshName";
            //refreshName.UpdateMode = UpdatePanelUpdateMode.Conditional;
            refreshName.UpdateMode = UpdatePanelUpdateMode.Always;
            refreshName.ChildrenAsTriggers = true;

            sayHello.Click += new EventHandler(sayHello_Click);


            refreshName.ContentTemplateContainer.Controls.Add(this.inputName);
            refreshName.ContentTemplateContainer.Controls.Add(sayHello);
            refreshName.ContentTemplateContainer.Controls.Add(this.displayName);

           // this.Controls.Add(scriptHandler);
            this.Controls.Add(refreshName);

        }
        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(AjaxWP1), "UpdatePanelFixup",
                //  fixupScript, true);

//아래와 같이 바꾸어 주어야 하는 것이죠..
                ScriptManager.RegisterStartupScript(this,
                  typeof(AjaxWP1), "UpdatePanelFixup",
                  "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;", true);
            }
        }

        void sayHello_Click(object sender, EventArgs e)
        {
            this.displayName.Text = "Hello," +
                this.inputName.Text.ToString() + ".";
        }

}
//이렇게 하면 몇번 눌러도 잘 된답니다.

관련글 더보기

댓글 영역