2012-07-20 54 views
1

(C#ASP網站)現在我有一個預先檢查過的CheckBoxList,取決於用戶在表單中選擇什麼。以下是編寫預檢的代碼以供參考。它完美的作品。每當一個新的預先檢查由用戶選擇=>回傳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; 
         } 
        } 
       } 
      } 
     } 

我遇到的問題是,用戶應該能夠在除了prechecked盒,檢查更多的箱子,但是當他們點擊提交按鈕發生原因,它不會檢測到新檢查的框。這裏是提交按鈕代碼。

 // 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); 

任何想法爲什麼提交按鈕不能看到新的檢查?它讀取預先檢查罰款。側面的問題 - 是否有更有效的方式來打開/關閉連接?是不錯的,我是新:)

+1

您的代碼預先檢查邏輯中的保護措施(測試回發,表單中的階段,其他狀態機狀態),以便您不覆蓋用戶選擇?請注意Init,OnLoad和CreateChildControls將始終在發生事件處理之前觸發。 – 2012-07-20 14:33:22

+0

我目前在頁面加載時發生了預檢,這顯然會消除任何用戶更改。你讓我思考這個,現在它完美的工作。謝謝您的幫助! – Mobius 2012-07-20 14:45:25

回答

1

簡單修復 - 我將PageLoad中的預檢代碼移到了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注入漏洞。

回答評論後,我們可以解決您的失敗檢查狀態更改。

相關問題