我知道 ASP.NET 页面生命周期,但我很困惑。我这里有从数据库记录创建按钮的代码。在我点击它们后,它们消失了,没有触发任何代码。:( 我知道我必须在 Page_Init 中重新创建它们,但我不知道如何。请帮忙!这是我的代码:
try
{
con.Open();
SqlDataReader myReader = null;
SqlCommand myCom = new SqlCommand("select ID,client from tposClient where CardNo='" + cNo + "'", con);
myReader = myCom.ExecuteReader();
Panel panel1 = new Panel();
panel1.Style["text-align"] = "Center";
panel1.Style["background"] = "blue";
div_login.Visible = false;
while (myReader.Read())
{
string b = myReader["client"].ToString();
string id = myReader["ID"].ToString();
Button btn = new Button();
btn.Text = b;
btn.ID = id;
btn.Style["width"] = "100px";
btn.Click += new EventHandler(btn_Click);
panel1.Controls.Add(btn);
panel1.Controls.Add(new LiteralControl("<br />"));
form1.Style.Add("display", "block");
form1.Controls.Add(panel1);
}
}
catch (Exception k)
{
Console.WriteLine(k.ToString());
}
finally
{
cmdselect.Dispose();
if (con != null)
{
con.Close();
}
}