
February 5th, 2002, 12:45 AM
|
|
Contributing User
|
|
Join Date: Dec 2002
Posts: 14,578
  
Time spent in forums: < 1 sec
Reputation Power: 22
|
|
|
Dynamically generated button does not work!!!
Originally posted by : Richard (xiao_john@yahoo.com)I am doing an e-learning web site. I need to read data out from the access database and then generate button accordingly. After clicking each button, a function is called, but parameter is different. I generate table dynamically and put the buttons inside, but after clicking nothing happens. Where goes wrong???Thanks!!!My Source Code: public string MyConnString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/elearning.mdb"; int i = 0; public void Page_Load(Object src,EventArgs e) { string strComm1 = "select Name from Personal where Priviledge=1 and Valid='Y'"; string strComm2 = "select Name from Personal where Priviledge=2 and Valid='Y'"; string strComm3 = "select Name from Personal where Priviledge=2 and Valid='?'"; string Label1 = "Registered Students:"; string Label2 = "Registered Teachers:"; string Label3 = "Teachers to be approved:"; Display_Page(strComm1, Label1); Display_Page(strComm2, Label2); Display_Page(strComm3, Label3); } void Display_Page(String strComm, String label_text) { OleDbConnection MyConnection = new OleDbConnection(MyConnString); MyConnection.Open(); OleDbCommand myCommand = new OleDbCommand(strComm, MyConnection); OleDbDataReader myReader; myReader = myCommand.ExecuteReader(); TableRow labelRow = new TableRow(); TableCell labelCell = new TableCell(); labelCell.Text = label_text; labelRow.Cells.Add(labelCell); myTable.Rows.Add(labelRow); if(myReader.Read()) { do { TableRow buttonRow = new TableRow(); TableCell dataCell = new TableCell(); dataCell.Text = myReader.GetString(0); buttonRow.Cells.Add(dataCell); TableCell buttonCell = new TableCell(); Button submitButton = new Button(); // Event handler for the button;submitButton.Click += new System.EventHandler(SubmitBtn_Click); submitButton.Width = 80; submitButton.Height = 25; submitButton.Visible = true; submitButton.CommandArgument = myReader.GetString(0); submitButton.Text = "Kick Out!"; submitButton.Style.Add("runat", "server"); buttonRow.Cells.Add(buttonCell); buttonCell.Controls.Add(submitButton); myTable.Rows.Add(buttonRow); i=i+1; } while(myReader.Read()); } else { // Code used to show alarm } myReader.Close(); MyConnection.Close(); } // Function suppose to act after click button;void SubmitBtn_Click(Object sender, EventArgs e) { Button control = ( Button ) sender //Message.Text = "Hey! Please enter your name in the textbox!"; control.Text="good"; }
|