2012-07-20 3 views
1

(C# ASP Site) 지금 사용자가 양식의 초기에 선택하는 내용에 따라 CheckBoxList가 사전 검사됩니다. 다음은 참조 용 사전 검사를 작성하는 코드입니다. 그것은 완벽하게 작동합니다. 새로운를 사전에 사용자 선택 => 다시 게시 난 데 문제는 사용자가 prechecked 상자에 추가로 더 상자를 검사 할 수 있지만,이 클릭 할 때 버튼을 제출해야한다는 것입니다CheckBoxList 선택 항목이 감지되지 않습니다.

 DataTable DefaultActivity = GetData(); 
     // Resets checkboxes to blank in between postbacks 
     for (int p = 0; p < CheckBoxList1.Items.Count; p++) 
     { 
      CheckBoxList1.Items[p].Selected = false; 
     } 
     // Fills in the prechecked boxes 
     if (DefaultActivity.Rows.Count > 0) 
     { 
      int SelectedRoleID = Int32.Parse(RadioButtonList2.SelectedValue); 
      for (int i = 0; i < DefaultActivity.Rows.Count; i++) 
      { 
       int DatabaseRoleID = Convert.ToInt32(DefaultActivity.Rows[i]["roleid"]); 
       if (SelectedRoleID == DatabaseRoleID) 
       { 
        int DatabaseActivityID = Convert.ToInt32(DefaultActivity.Rows[i]["activityid"]); 
        for (int j = 0; j < CheckBoxList1.Items.Count; j++) 
        { 
         if (DatabaseActivityID == Convert.ToInt32(CheckBoxList1.Items[j].Value)) 
         { 
          CheckBoxList1.Items[j].Selected = true; 
         } 
        } 
       } 
      } 
     } 

에 의해 원인이 발생할 때마다 새로 검사 한 상자는 검색하지 않습니다. 제출 버튼 코드는 다음과 같습니다.

 // Data into the request table 
     SqlConnection sqlConn = new SqlConnection("Server=x;Database=x;User=x;Password=x;Trusted_Connection=False;"); 
     string Statement = "INSERT INTO table (x) OUTPUT INSERTED.requestid VALUES (x)"; 
     SqlCommand sqlComm = new SqlCommand(Statement, sqlConn); 

     // Grabs the auto-created RequestID to forward to the next page 
     sqlComm.Connection.Open(); 
     string RequestID = Convert.ToString((Int32)sqlComm.ExecuteScalar()); 
     sqlComm.Connection.Close(); 
     sqlComm.Connection.Dispose(); 

     // Data into the x table 
     SqlConnection ActivitysqlConn = new SqlConnection("Server=x;Database=x;User=x;Password=x;Trusted_Connection=False;"); 
     string ActivityStatement = "INSERT INTO table (x) VALUES (x)"; 
     for (int k = 0; k < CheckBoxList1.Items.Count; k++) 
     { 
      if (CheckBoxList1.Items[k].Selected == true) 
      { 
       SqlCommand ActivitysqlComm = new SqlCommand(ActivityStatement, ActivitysqlConn); 
       ActivitysqlComm.Connection.Open(); 
       ActivitysqlComm.ExecuteNonQuery(); 
       ActivitysqlComm.Connection.Close(); 
      } 
     } 
     Response.Redirect("nextscreen.aspx?RequestID=" + RequestID); 

제출 버튼에 새 수표가 표시되지 않는 이유는 무엇입니까? 그것은 prechecks 벌금을 읽습니다. 측면 질문 - 연결을 열거 나 닫는 것보다 효율적인 방법이 있습니까? 좋은, 나는 새로운 :

+1

사용자 선택을 덮어 쓰지 않도록 논리가 사전 검사 (포스트 백 테스트, 양식 단계, 일부 다른 상태 시스템 상태)하는 코드입니까? 이벤트 처리가 발생하기 전에 Init, OnLoad 및 CreateChildControls가 항상 실행된다는 점에 유의하십시오. –

+0

현재 pageload에서 prechecking이 발생 했으므로 사용자 변경 사항이 지워집니다. 당신은 저에 대해 생각하고 지금 완벽하게 작동합니다. 도와 주셔서 감사합니다! – Mobius

답변

1

쉬운 픽스 - 미리 검사 코드에 PageRoad에서 RadioButtonList로 이동했습니다.이 개체는 사전 검사에 영향을줍니다. 이제 모든 사전 검사와 추가 검사가 올바르게 저장됩니다.

1

이 SQL의 질문에 대답 해요, 이것은 "깨끗한"방법 :

using (SqlConnection connection = new SqlConnection(connectionString)) 
{ 
    SqlCommand command = connection.CreateCommand(); 

    command.CommandText = "mysp_GetValue"; 
    command.CommandType = CommandType.StoredProcedure; 

    connection.Open(); 
    object ret = command.ExecuteScalar(); 
} 

당신은 구성에서 연결 문자열을 저장하는 곳뿐만 아니라 저장 프로 시저를 사용한다 SQL 주입 취약점을 완화하는 데 도움이되는 곳입니다.

댓글에 답한 후에는 실패한 수표 상태 변경 사항을 해결할 수 있습니다.

관련 문제