2011-04-18 7 views
3

어떤 버튼을 클릭했는지 파악하려고하는데이 코드는 IE에서 제대로 작동하지만 Chrome, Firefox 또는 Safari라면 아무 것도하지 않습니다. 파이어 폭스에서 파이어 버그를 사용할 때 폼 세부 정보를 살펴본 결과, EVENTTARGET에는 값이 없음을 알 수 있습니다. FF, Chrome 및 Safari에서 어떻게 작동합니까?EVENTTARGET 보낸 사람을 확인하는 중 문제가 발생했습니다.

방법 : 메서드를 호출

 Control postbackControlInstance = null; 

     string postbackControlName = page.Request.Params.Get("__EVENTTARGET"); 
     if (postbackControlName != null && postbackControlName != string.Empty) 
     { 
      postbackControlInstance = page.FindControl(postbackControlName); 
     } 
     else 
     { 
      for (int i = 0; i < page.Request.Form.Keys.Count; i++) 
      { 
       postbackControlInstance = page.FindControl(page.Request.Form.Keys[i]); 
       if (postbackControlInstance is System.Web.UI.WebControls.Button) 
       { 
        return postbackControlInstance; 
       } 
      } 
     } 
     if (postbackControlInstance == null) 
     { 
      for (int i = 0; i < page.Request.Form.Count; i++) 
      { 
       if ((page.Request.Form.Keys[i].EndsWith(".x")) || (page.Request.Form.Keys[i].EndsWith(".y"))) 
       { 
        postbackControlInstance = page.FindControl(page.Request.Form.Keys[i].Substring(0, page.Request.Form.Keys[i].Length - 2)); 
        return postbackControlInstance; 
       } 
      } 
     } 
     return postbackControlInstance; 

코드 :

 if (Page.IsPostBack) 
     { 
      try 
      { 
       Control cause = GetPostBackControl(Page); 
       string statecause = cause.ID; 
       if (statecause == "buttonName1") 
       { 
        search(statecause); 
       } 
       else if (statecause == "buttonNAME2") 
       { 
        resetfields(); 
       } 
      } 
      catch { } 
     } 

답변

3

가장 좋은 방법은 다시 게시가 protectedPage.RaisePostBackEvent 메소드를 오버라이드 (override)하는 원인이 무엇인지 제어 결정하는 것입니다. 클라이언트 측 __doPostBack 기능이 때

public class MyPage : Page 
{ 
    protected override void RaisePostBackEvent(
     IPostBackEventHandler sourceControl, 
     string eventArgument 
    ) 
    { 
     // here is the control that caused the postback 
     var postBackControl = sourceControl; 

     base.RaisePostBackEvent(sourceControl, eventArgument); 
    } 
} 

당신이 scenarious을 위해 일한다 제공하는 코드 :이 방법은 들어오는 포스트 백 이벤트를 처리해야하는 포스트 백을 발생하는 서버 컨트롤을 알리기 위해 ASP.NET의 기반 시설에 의해 사용된다 페이지에 렌더링됩니다 (예 : <asp:Button runat="server" ID="btnSubmit" Text="submit" UseSubmitBehavior="true" />과 같은 버튼 하나만 사용하는 경우 렌더링되지 않음).

심지어 경우에 __doPostBack 기능을 렌더링하는 경우

하지만 __EVENTTARGET 매개 변수는 __doPostBack 기능의 기본 동작을 사용자 정의/대부분의 경우 자바 스크립트 코드 호환에 의해 침해되는 것을 의미 비어 있습니다. 이 경우 ASP.NET 인프라조차도 포스트 백 이벤트를 올바르게 처리 할 수 ​​없습니다.

관련 문제