2017-03-11 15 views
-1

아래 코드는 Asp.Net에서 yes no를 사용하여 확인 메시지 상자를 실행합니다.
그 값이 확증되었는지 아닌지를 알아 내야합니다.
어떻게하면됩니까?메시지 박스가 올바르게 작동하지 않는 것을 확인하십시오.

영문 :

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
    <script type = "text/javascript"> 
     function Confirm() { 
      var confirm_value = document.createElement("INPUT"); 
      confirm_value.type = "hidden"; 
      confirm_value.name = "confirm_value"; 
      if (confirm("Do you want to save data?")) { 
       confirm_value.value = "Yes"; 
      } else { 
       confirm_value.value = "No"; 
      } 
      document.forms[0].appendChild(confirm_value); 
     } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" OnClientClick = "Confirm()"/> 
    </form> 
</body> 
</html> 

코드 뒤에 :

protected void OnConfirm(object sender, EventArgs e) 
{ 
    // This method runs even though the user clicks no. 
} 

업데이트 :이 코드
, 모두 예 또는 아니오 선택은 OnConfirm 이름이 같은 방법으로 실행됩니다. 그래서 OnConfirm 메서드 only if the user clicks yes을 실행 해 봅니다.

+0

는 [내가 여기 해결책을 발견했다 (http://stackoverflow.com/a/9835475/1136253) –

답변

0

업데이트 :

runat = server와 함께 숨김 필드를 사용하고 예/아니요를 저장할 수 있습니다. 그런 다음 서버로 보냅니다.

<input type="hidden" runat="server" value="" id="hidden1" /> 


function Confirm() { 

     if (confirm("Do you want to save data?")) { 
      confirm_value.value = "Yes"; 
      document.getElementById("hidden1").value = "yes"; 
     } else { 
      confirm_value.value = "No"; 
      document.getElementById("hidden1").value = "no"; 
     } 
     document.forms[0].appendChild(confirm_value); 
    } 

마스터 페이지를 사용하는 경우 숨겨진 클라이언트 ID가 해당 서버 ID와 다릅니다.

protected void OnConfirm(object sender, EventArgs e) 
{ 
    string confirmValue = hidden1.Value;  
} 
+0

내 업데이트를 참조하십시오. –

0

사용자가 올바르게 확인하면 서버 측 버튼 만 실행한다고 생각합니까? 네, 그럼 그냥 이렇게하면 자바 스크립트 함수가 false를 반환 할 때, 기본적으로 서버 사이드 버튼은 발생하지 않습니다 즉 : (OnClientClick = "반환 확인()")

<script type = "text/javascript"> 
     function Confirm() {   
      return confirm("Do you want to save data?"); 
     } 
    </script> 


    <asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" OnClientClick = "return Confirm()"/>