2012-01-24 2 views
1

내가하는 wizardcontrol을 데 여기서 한 단계 사용자가 입력 할 수 있습니다 자신의 이름, EMAILADDRESS, PHONENUMBER 등 사용자 프레스, 나는 마법사의 "다음"버튼을 해요자바 스크립트 : 마법사

에서 지정된 전화 번호를 가진 excisting 계정이 있는지보기 위하여 데이타베이스를 검사하십시오.

이 경우 시스템은 사용자에게 새 정보를 해당 번호에 바인딩할지 또는 새 전화 번호를 입력 할지를 묻는 메시지를 표시합니다.

정보를 바인딩하겠다고 말하면 정보는 바인딩되고 마법사는 2 단계로 진행하고 새로운 전화 번호를 입력하면 1 단계에서 마법사를 계속 진행합니다.

protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e) 
{ 
    if (Wizard1.ActiveStepIndex == 0) 
    { 
     Page.Validate(); 
     if (Page.IsValid) 
     { 
      if (!Mobile_ValidateForExistingUsers(((TextBox)WizardStep1.ContentTemplateContainer.FindControl("txtPhone")).Text)) 
      { 
     //JavaScript popup or something, which prompts the user? 
      } 
     } 
    } 
} 

validater은 다음과 같습니다 :

코드는 다음과 같이 보입니다 사용자가이 질문을하고, 중 진행 또는 그에게 몇 가지 새로운 정보를 입력 할 수 있도록하는 방법

protected bool Mobile_ValidateForExistingUsers(string usrPhone) 
{ 
    bool IsValid = false; 

    using (SqlConnection conn = new SqlConnection(connString)) 
    using (SqlCommand cmd = new SqlCommand("spCheckMobile", conn)) 
    { 
     cmd.CommandType = System.Data.CommandType.StoredProcedure; 
     cmd.Parameters.Add(new SqlParameter("@Mobile", usrPhone)); 

     cmd.Connection.Open(); 

     object result = cmd.ExecuteScalar(); 

     if (result == null) 
     { 
      IsValid = true; 
     } 
    } 
    return IsValid; 
} 

마법사?

답변

0

이 문제를 처리하는 가장 효과적인 방법은 Mobile_ValidateForExistingUsers(phoneNumber)이 AJAX 호출을 위해 WebMethod로 기능하도록하는 것이라고 생각합니다. 일단 AJAX 코드를 작성하면 AJAX 호출을 <asp:CustomValidator>에 연결하여 클라이언트 측 유효성 검사 기능을 수행하고 false를 반환하면 window.alert('use new number')을 후속 조치하십시오.

또는 서버 측 코드를 <asp:CustomValidator>으로 처리 할 수도 있지만 유효성 검사 서버 측을 처리하는 경우 클라이언트 측 유효성 검사 기능을 설정할 필요는 없습니다. CustomValidator1.IsValid 속성을 Mobile_ValidateForExistingUsers(phoneNumber) 함수의 결과로 설정 한 다음 Page.Validate()을 호출하여 해당 페이지가 여전히 유효한지 확인하십시오. 그렇지 않은 경우 runat="server" 숨겨진 입력을 플래그로 지정하고 일부 클라이언트 측 코드에서 window.onload으로 사용자에게 메시지를 보내고 새 번호를 사용하려면 두 번째 숨겨진 필드에 저장하십시오. 예는 다음과 같습니다

protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e) 
{ 
    if (Wizard1.ActiveStepIndex == 0) 
    { 
     Page.Validate(); 
     if (Page.IsValid) 
     { 
      if (HiddenField2.Value == '') //Client-side Flag not set 
      { 
       if (!Mobile_ValidateForExistingUsers(((TextBox)WizardStep1.ContentTemplateContainer.FindControl("txtPhone")).Text)) 
       { 
        CustomValidator1.IsValid = false; // Invalidate the page so the Next Step fails. 
        HiddenField1.Value = false;   // Page will get re-rendered to client to fix errors and stay on the same step. 
       } 
      } 
      else 
      { 
       if (HiddenField2.Value == 'true') 
       { 
        // Use new binding logic. 
       } 
       else 
       { 
        //User does not want to use new number, so logic to handle goes here. 
       } 
      } 

     } 
    } 
} 

및 다음 어딘가에 클라이언트 측 마크 업 :이 도움이

/* 
* Crude example 
*/ 
<script type="text/javascript"> 
    window.onload = function() { 
     'use strict'; 
     var hf1 = document.getElementById('<%=HiddenField1.ClientID %>'); 
     var hf2 = document.getElementById('<%=HiddenField2.ClientID %>'); 
     var myForm = document.getElementById('<%=Form1.ClientID %>'); 
     if (hf1.value === 'false') { // or if (hf1.value) if you just want the truthiness of the value's existence (depending on how you flag stuff) 
      hf2.value = window.confirm('Wanna use this number?').toString().toLowerCase(); 
     } 
     myForm.submit(); 
    } 
</script> 

희망,

피트