1

(C#ASPサイト)現在、ユーザーがフォームの早い段階で何を選択したかに応じて、事前にチェックされた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);

送信ボタンが新しいチェックを表示できない理由はありますか?事前チェックを正常に読み取ります。副次的な質問-接続を開閉するためのより効率的な方法はありますか?いいね、私は新しいです:)

4

2 に答える 2

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インジェクションの脆弱性を軽減するために、可能な場合はストアドプロシージャを使用するだけでなく、接続文字列を構成のどこかに格納する必要があります。

コメントに回答した後、失敗したチェック状態の変更に対処できます。

于 2012-07-20T14:37:40.823 に答える
1

簡単な修正-事前チェックコードをPageLoadからRadioButtonListに移動しました。これは、事前チェックに影響を与えるオブジェクトです。これで、すべての事前チェックと追加のチェックが適切に保存されます。

于 2012-07-20T14:42:43.363 に答える