2

イベントが発生しない理由と、イベントを発生させたチェックボックス コントロールを見つける方法を知りたいです。

chkList1 = new CheckBox();
                            chkList1.Text = row["subj_nme"].ToString();
                            chkList1.ID = row["subjid"].ToString();
                            chkList1.Checked = true;
                            chkList1.Font.Name = "Verdana";
                            chkList1.Font.Size = 12;
                            chkList1.AutoPostBack = true;
                            chkList1.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
                            Panel1.Controls.Add(chkList1);

protected void CheckBox_CheckedChanged(object sender, EventArgs e)
            {
                Label1.Text = "Called";
            }
4

1 に答える 1

2

イベントが発生しない場合は、次の 2 つの理由のいずれかが考えられます。

  1. コントロールがページのライフサイクルで再作成されるのが遅すぎます。中にコントロールを作成してみてくださいOnInit
  2. 検証によりポストバックが妨げられています。これを回避するCausesValidationには、すべての CheckBox コントロールを false に設定します。

sender引数を使用して、イベントをトリガーしたコントロールを見つけることができます。

protected void CheckBox_CheckChanged(object sender, EventArgs e)
{
    //write the client id of the control that triggered the event
    Response.Write(((CheckBox)sender).ClientID);
}
于 2012-05-06T22:48:36.080 に答える