2012-07-20 33 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注入漏洞。

回答评论后,我们可以解决您的失败检查状态更改。