动态创建按钮点击事件不起作用

时间:2018-12-29 11:51:09

标签: c# asp.net

我在页面加载和按钮单击事件上动态生成按钮,我调用LoadSubClause事件运行良好,并且在此事件中,我正在创建新按钮。这些新创建的按钮在单击按钮时分配了LoadDescription事件,但是一旦我单击这些按钮中的任何一个,它就会清除SubClause创建的按钮,并且LoadDescription事件中没有任何显示。

  

public void LoadSubClause(object sender, EventArgs e)
{
    Button btn = sender as Button;
    string ClauseName = btn.Text.ToString();
    int counter = 100;

    string query = "select SubClause from Output_DocumentContent where Clause ='" + ClauseName.ToString() + "'";
    Response.Write(query);
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Legal;Integrated Security=true");
    SqlCommand cmd = new SqlCommand(query, con);
    con.Open();
    SqlDataReader sdr = cmd.ExecuteReader();

    while (sdr.Read())
    {
        Button newButton = new Button();
        newButton.Attributes["class"] = "buttonVertical";
        newButton.Text = sdr[0].ToString();
        newButton.ID = "SubClause"+counter.ToString();
        newButton.Click += new EventHandler(LoadDescription);
        //buttons.Add(newButton);
        PanelSubClause.Controls.Add(newButton);
        counter++;
    }
    con.Close();
}
public void LoadDescription(object sender, EventArgs e)
{
    Response.Write("Load Description Event");
}

0 个答案:

没有答案