日期:2014-05-18 浏览次数:20658 次
protected void Page_Load(object sender, EventArgs e) { sda = new SqlDataAdapter("select faci_id, faci_name from facilities where display='1'", conn); sda.Fill(ds, "facilities"); for (int i = 0; i < ds.Tables["facilities"].Rows.Count; i++) { myButton = new Button(); myButton.Width = 85; myButton.Text = ds.Tables["facilities"].Rows[i][1].ToString(); myButton.ID = ds.Tables["facilities"].Rows[i][0].ToString(); myButton.Click += new EventHandler(myButton_Click); this.Panel1.Controls.Add(myButton); } } protected void myButton_Click(object sender,EventArgs e) { Button myBtn = (Button)sender; selectButton(myBtn.Text.ToString().Trim()); Label1.Text = "您点击了" +myBtn.Text.ToString()+ "这个按钮"; } private void selectButton(string myButtonText) { ds.Tables.Clear(); string sql = "SELECT * FROM faci_orders INNER JOIN facilities ON faci_orders.faci_id = "+ "facilities.faci_id WHERE (facilities.faci_name = '" + myButtonText + "')"; sda = new SqlDataAdapter(sql,conn); sda.Fill(ds,"faci_orders"); //动态添加命令按钮并添加属性事件 for (int i = 0; i < ds.Tables["faci_orders"].Rows.Count; i++) { myButton = new Button(); myButton.Text = ds.Tables["faci_orders"].Rows[i]["order_name"].ToString(); myButton.ID = ds.Tables["faci_orders"].Rows[i]["order_id"].ToString(); //设置按钮大小以及委托事件 myButton.Width = 85; myButton.Height = 30; myButton.Click += new EventHandler(orderBtn_Click); Panel2.Controls.Add(myButton); } } protected void orderBtn_Click(object sender,EventArgs e) { //根据所点击按钮传送指令 Button myOrderBtn = (Button)sender; Label2.Text =“你点击了按钮:"+ myOrderBtn.Text.ToString();//没反应了?? }