2014-09-02 1 views
2

저는 C#/Net을 사용하여 인트라넷 용으로 작성한이 응용 프로그램을 가지고 있습니다. 한 곳에서 OnLeave 이벤트의 값을 검사하여 일부 입력란을 자동 완성합니다. 문제는 사용자가 필드에서 제출 단추로 직접 이동하면 OnLeave 이벤트가 발생하지만 필드가 채워지지 않는다는 것입니다.어떻게 C# 웹 응용 프로그램을 "따라 잡을"수 있습니까?

VBA의 DoEvents에 대해 생각해 보았습니다. "Application.DoEvents"를 사용하십시오.하지만 시도 할 때 빨간색으로 강조 표시됩니다. 나는 또한 "Render()"라는 것을 발견했다. 그러나 나는 그것을 시도 할 때 푸른 색으로 밑줄을 긋는다.

이 코드를 "따라 잡아"모든 데이터가 올바르게 렌더링되도록 사용자에게 알려줄 수 있습니까? 제 생각에는 아직도 C#에 익숙하지 않습니다. 가능한 한 명시 적/철저한 수 있다면 감사하겠습니다.

편집

나는 텍스트 상자의 OnLeave 이벤트로이 코드를 가지고 :

protected void txtClientID_OnLeave(object sender, EventArgs e) 
    { 
     using (SqlConnection con = new SqlConnection(str2)) 
     { 
      //Query the Reports table to find the record associated with the selected report 
      using (SqlCommand cmd = new SqlCommand("SELECT * FROM VW_MOS_DPL_AccountValidation WHERE CLIENT_ID = '" + txtClientID.Text + "'", con)) 
      { 
       con.Open(); 
       using (SqlDataReader DT1 = cmd.ExecuteReader()) 
       { 
        // If the SQL returns any records, process the info 
        if (DT1.HasRows) 
        { 
         while (DT1.Read()) 
         { 
          try 
          { 
           int TaskID = Convert.ToInt32(ddlTask.SelectedValue); 

           // This should allow Client ID to autofill if Eligibility --> Enrollment records are used. 
           // Add more Task IDs to this list if necessary. 
           List<int> list = new List<int>() { 154, 156, 157, 158, 160, 161, 165 }; 
           if (list.Contains(TaskID)) 
           { 
            //lblAccountName.Text = (DT1["CUST_NM"].ToString()); 
            Label2.Text = (DT1["CUST_NM"].ToString()); 
            //lblAccountName.Visible = true; 
            TBAccountNum.Text = (DT1["CUST_NUM"].ToString()); 
            TBAccountNum.Visible = true; 
           } 
          } 
          catch (Exception ae) 
          { 
           Response.Write(ae.Message); 
          } 
         } 
        } 
        // If the SQL returns no records, return a static "No Records Found" message 
        else 
        { 
         //lblAccountName.Text = "No Matching Account Name"; 
         Label2.Text = "No Matching Account Name"; 
         //lblAccountName.Visible = true; 
         TBAccountNum.Text = ""; 
        } 
       } 
      } 
     } 
    } 

나는 또한 버튼을 제출해야하고, 버튼을 누를 때, 제일 먼저 내가 원하는을 이 OnLeave로 채워지도록되어있는 모든 필드가 실제로 채워지는지 확인해야합니다. 문제는 내가 단계별로 살펴보면 모두 값이 있지만, 방금 실행하면 값이 화면에 나타나지 않습니다. .

"System.Threading.Thread.Sleep (100);" 다른 사이트에서 본 추천에 따르면,하지만 그건 아무 것도하지 않는 것 같습니다.

+2

내가 코드를 게시하는 것이 좋을 것이라고 생각합니다. –

+1

필드가 비어 있으면 제출 클릭시 자동 채우기 코드가 실행됩니까? 어떤 코드 없이는 무슨 일이 벌어지고 있는지 알기가 어렵습니다. –

+0

ASP.NET Web Forms을 사용하고 있습니까? 2014 년에? – bzlm

답변

0

코드를 RefreshData 또는 그 자체의 메소드로 이동할 수 있습니다. txtClientID_OnLeave 안에이 메서드를 호출하십시오. 그런 다음 버튼 제출 이벤트에서 해당 텍스트 상자가 비어 있는지 확인하십시오. 그런 경우 클릭 이벤트에서 다른 작업을 수행하기 전에 RefreshData 메소드를 호출하십시오.

RefreshData이 호출 될 때 조금 더 가서 플래그를 설정하고 텍스트 상자가 비어 있는지 확인하는 대신 단추 제출 이벤트에서이 플래그를 확인하십시오. 사용자가 텍스트 상자에 글을 쓰면 제출 클릭은 그렇지 않은 경우 데이터를 검색하지 않습니다.

private bool _retrievedData = false; 

public void RefreshData() { 
    // do everything you were doing inside the `txtClientID_OnLeave` handler 
    // make sure to set this flag only if the data was successfully retrieved 
    // the bottom of the `try` should be good 
    _retrievedData = true; 
} 

public void txtClientID_OnLeave(object sender, EventArgs e) { 
    RefreshData(); 
} 

public void yourButton_Click(object sender, EventArgs e) { 
    if (_retrievedData == false) 
     RefreshData(); 

    // do whatever you were doing in this handler because now your textboxes have the 
    // data it would have if you had left the textbox without going straight to submit 
} 

지금 내가이 처리하는 청소기 방법이 확신하지만 제출이 텍스트 상자를 읽고 있기 때문에 핸들러가 일을 제출하기 전에 당신이 정말로 필요한 것은 텍스트 상자를 입력하는 것입니다.

관련 문제