달력

42024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

사용자 컨트롤이든 커스텀 컨트롤이든 어째뜬간에 event 등록 시키는 것..

        private event EventHandler dataBoundEvent;
        public event EventHandler DataBound
        {
            add { dataBoundEvent = value; }
            remove { dataBoundEvent = null; }
        }
Posted by SooYeol
|


잘못된 다시 게시 또는 콜백 인수입니다. 이벤트 유효성 검사는 구성의 <pages enableEventValidation="true"/> 또는 페이지의 <%@ Page EnableEventValidation="true" %>를 사용하여 활성화됩니다. 이 기능은 다시 게시 또는 콜백 이벤트에 대한 인수가 원래 이들을 렌더링한 서버 컨트롤에서 발생하는지 확인하여 보안을 유지합니다. 데이터가 올바르면 유효성 검사에 대한 다시 게시 또는 콜백 데이터를 등록하는 데 ClientScriptManager.RegisterForEventValidation 메서드를 사용합니다.


asp.net 을 하다보면 자주 나오는 오류 중 하나.

오류에 친절하게 설명 되 있는 것처럼 enableEventValition 을 바꿔줄수도 있지만..  이건 뭔가 마음에 안들었음.
또한 ClientScriptManager 로 오류나는 녀석을 이벤트로 등록시켜주는 방법도 있었지만 역시 마음에 안들었음..
일딴 원인이 무엇인지 찾아보려고 고민..

그래서 찾은 원인은 페이지 안에 들어있는 컨트롤이 달라져서 나오는 오류.

내가 저런 오류가 나왔던 이유는 PostBack 됐을때 Page_Load 시에 GridView를 다시 DataBind()를 시켰었음.
그래서 GridView 외부에서 PostBack이 일어나는 것에 대해서는 문제 없이 잘 됐지만 GridView 내부의 컨트롤로 PostBack 시킬 때 문제 발생.

asp.net 에서 Page_Load 가 있고 나서 eventhandler를 수행하기에 발생되는 문제였음.
즉, GridView 를 다시 DataBind 하고 나면 GridView가 새로 그려지기 때문에 원래 있었던 컨트롤과 달라져 수행한 이벤트의 컨트롤이 없어져버림...


그러한 Page_Load 시 DataBind()를 처리 할 때 예외로 특정 컨트롤 ID인 경우는 DataBind 를 시키지 않도록 수정 했더니 정상 동작 됨.


PostBack Event를 시킨 컨트롤을 찾으려면 아래 코드를 사용하면 됨.
출처 : http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx
=====================================================================================================================
        private string getPostBackControlName()
        {
            Control control = null;
            //first we will check the "__EVENTTARGET" because if post back made by       the controls
            //which used "_doPostBack" function also available in Request.Form collection.
            string ctrlname = Page.Request.Params["__EVENTTARGET"];
            if (ctrlname != null && ctrlname != String.Empty)
            {
                control = Page.FindControl(ctrlname);
            }
            // if __EVENTTARGET is null, the control is a button type and we need to
            // iterate over the form collection to find it
            else
            {
                string ctrlStr = String.Empty;
                Control c = null;
                foreach (string ctl in Page.Request.Form)
                {
                    //handle ImageButton they having an additional "quasi-property" in their Id which identifies
                    //mouse x and y coordinates
                    if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
                    {
                        ctrlStr = ctl.Substring(0, ctl.Length - 2);
                        c = Page.FindControl(ctrlStr);
                    }
                    else
                    {
                        c = Page.FindControl(ctl);
                    }
                    if (c is System.Web.UI.WebControls.Button ||
                             c is System.Web.UI.WebControls.ImageButton)
                    {
                        control = c;
                        break;
                    }
                }
            }
            return control.ID;

        } 

=====================================================================================================================


음... 근데 지금 와서 해결하고 나서 생각해보니... ClientScriptManager.RegisterForEventValidation 여기에 등록하는거랑 차이점이 뭔가-_-;;;
그냥 버튼 렌더링 시킬 때 저기다 등록시키는게 더 나을려나-_-;;;

>> ClientScriptManager.RegisterForEventValidation 이건 render할때밖에 동작 못시킴..
그니까 GridView 안에 들어있는 컨트롤에 대해서 render 이벤트를 재정의 하려면 상당히 귀찮을듯.. -ㅁ-

Posted by SooYeol
|