protected void buttonlook_Click(object sender, EventArgs e)
{
string sqlstring = "select * from liuyan ";
DataBase db = new DataBase();
DataTable dt = db.GetDataTable(sqlstring);
Button [] bt = new Button[50];
Label[] lb = new Label[50];
Panel[] pa = new Panel[50];
int l = 0;
for (int i = 0; i < dt.Rows.Count && i < 50; i++)
{
l = i + 1;
lb[i] = new Label();
bt[i] = new Button();
pa[i] = new Panel();
System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#000080");
lb[i].ForeColor = c;
lb[i].Font.Bold = true;
lb[i].Text = "留言内容:" + " " + "第" + l + "条" + "<br>"
+ "<br>" + "姓名:" + " " + dt.Rows[i]["mingcheng"].ToString() + "<br/>"
+ "电话号码:" + " " + dt.Rows[i]["haoma"].ToString() + "<br/>"
+ "地址:" + " " + dt.Rows[i]["dizhi"].ToString() + "<br/>"
+ "电子邮件:" + " " + dt.Rows[i]["youjian"].ToString() + "<br/>"
+ "留言内容: " + " " + dt.Rows[i]["liuyan"].ToString () + "<br/>";
bt[i].ID = dt.Rows[i]["bianhao"].ToString () ;
bt[i].Text = "删除" ;
pa[i].Controls.Add(lb[i]);
pa[i].Controls.Add(new LiteralControl("<br><br>"));
pa[i].Controls.Add(bt[i]);
pa[i].BorderWidth = 1;
pa[i].Width = 500;
bt[i].Click += new System.EventHandler(guanli_Click);//没有提示错误,就是点击了动态生成的按钮后,不执行下面的guanli_click方法
Panel1.Controls.Add(new LiteralControl("<br><br>"));
Panel1.Controls.Add(pa[i]);
Panel1.Controls.Add(new LiteralControl("<br><br>"));
}
}
void guanli_Click(object sender, EventArgs e)
{
Button button = sender as Button;
int i = Convert.ToInt32(button.ID);
string sqlstring = "delete from tupian where bianhao = " + i;
DataBase db = new DataBase();
int chenggong = db.ExecuteSQL(sqlstring);
if (chenggong != -1)
{
//System.Web.UI.Page.Response.Write("<Script Language=JavaScript>alert('删除成功!')</script>");
}
else
{
//System.Web.UI.Page.Response.Write("<Script Language=JavaScript>alert('删除失败,请重试!')</script>");
}
}