-3

一連のチェックボックス「はい/いいえ」と最後に送信ボタンがある調査スタイルの Web ページがあります。box1 が選択されているときに box2 のチェックを外したいと思います。

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {

        if (CheckBox1.Checked == true)
        {
          CheckBox2.Enable = false;
        }
    }

    public void Submit_Click(object sender, EventArgs e)
    {

    //Question1 - Checkbox1

        if (CheckBox1.Checked == true)
        {

            SqlConnection con = new SqlConnection("");
            String sql = "UPDATE INQUIRY2 set Question1 = @str WHERE email = @email AND base = @base;";
            ...
        }
4

1 に答える 1

2

There are a couple of things to note here...

  • In an ASP.NET environment, the CheckChanged event of the Checkbox will not fire until a postback is done;
  • Secondly, you likely want to use a RadioButtonList to manage this behaviour (though not necessarily).

In the first instance, that is to say that you can't expect the event to fire immediately after the checkbox state has changed, only once the form is submitted, either automatically using AutoPostBack or manually using a Button.

In the second case, radio buttons will be useful if these items are categorised together in a group but not really if they just mutually exclusive values on a form not otherwise related. We might be able to tell if you'd have given the controls meaningful names, alas.

A couple of other things while here anyway. You don't need to explicitly state the comparable boolean value in the conditions - given that the property is named somewhat appropriately, it's telling what if (CheckBox.Checked) { } is. Also, SqlConnection is a disposable thing, and it's not clear from your example whether you properly close or dispose of the instance, so I'd check you are doing so.

于 2013-08-27T15:36:05.613 に答える