Setting 'OnClick' for dynamic button in Code behind
-
All, I've gotten myself stuck on this problem and I can't seem to find a solution.:confused: I have an initial data bound radio button list(rbl) in a table that has an OnSelectedIndexChanged event that fires at the push of a button. Next I have the code that adds a child rbl and a button that would fire a subsequent OnSelectedIndexChanged for the child rbl. My question is this - How do I set the OnClick event for the dynamically created button? Yes I know that this could be solved with a tree view control, but I'm forced to write this code in 1.1 which doesn't have this control. Here is the code -
public void rbLocation_OnChange(object sender, System.EventArgs e) { DBAccess OBJBO = new DBAccess(); DataSet BaseNodeSet = new DataSet(); //insert code to add new table row Table tbl = new Table(); tbl = tblLoc; TableRow tr = new TableRow(); TableRow tr2 = new TableRow(); int numRows = tbl.Rows.Count; int iteration = numRows + 1; tbl.Rows.AddAt(numRows,tr); tbl.Rows.AddAt(iteration,tr2); //adding tablecell info TableCell td = new TableCell(); TableCell td2 = new TableCell(); TableCell td3 = new TableCell(); td.Width = 30; tr.Cells.Add(td); tr.Cells.Add(td2); //adding the radiobuttonlist for the children RadioButtonList rbChild = new RadioButtonList(); rbChild.Attributes["OnSelectedIndexChanged"] = "rbChild_OnChange()"; rbChild.DataTextField = "Description"; rbChild.DataValueField = "NodeID"; rbChild.ID = "rbChild" + iteration; rbChild.DataSource = OBJBO.GetBaseNodes(Convert.ToInt32(rbLocation.SelectedValue),3,1,out BaseNodeSet); rbChild.DataBind(); td2.Controls.Add(rbChild); //maintain the selected parent value & disable parent radiobuttonlist rbLocation.SelectedValue = rbLocation.SelectedValue; rbLocation.Enabled = false; btnOK.Visible = false; //add button that reverts to the parent radio list tr2.Cells.Add(td3); Button bk = new Button(); bk.ID = "btnBack"; bk.Text = "Previous Group"; bk.Attributes["OnClick"] = "btnBack_OnClick()"; bk.Visible = true; td3.Controls.Add(bk); }
An American football fan - Go Seahawks! Lil Turtle