Click event of a button contained in a tablecell of a asp.net table
-
I have an asp.net application. In response to a button(on the form) click event I create a asp.net table and create rows and column. These rows and coulmn's data I extract from dataset and fill the contnet of columns and rows of the table. As I want to have each row vertically displayed, I use asp.table control instead of DataGrid control. At the end of each row I have embeded a button control in last column. Code is as follwos: private void btnRunTcl_Click(object sender, System.EventArgs e) { DataSet ds = Test(); int counter = 0; //loop over columns in a row foreach(DataColumn dc in ds.Tables[0].Columns) { TableRow trow = new TableRow(); TableCell tcellcolname; tcellcolname = new TableCell(); tcellcolname.Text = dc.ColumnName; trow.BackColor = System.Drawing.Color.Beige; tcellcolname.BackColor = System.Drawing.Color.AliceBlue; //add column name to row tcellcolname.Controls.Add(new LiteralControl(dc.ColumnName.ToString())); trow.Cells.Add(tcellcolname); TableCell tcellcoData; //count the nr of columns to be added as rows int cnt = ds.Tables[0].Columns.Count; //loop over rows in table foreach(DataRow dr in ds.Tables[0].Rows) { tcellcoData = new TableCell(); //add column content to row tcellcoData.Controls.Add(new LiteralControl(dr[dc.ColumnName].ToString())); trow.Cells.Add(tcellcoData); counter++; //after last row(column) add button if(cnt == counter) { btn.ID="btnViewDetails"; btn.Font.Bold=true; btn.Text="ViewDetails"; TableCell tcellcoData1 = new TableCell(); tcellcoData.Controls.Add(btn); trow.Cells.Add(tcellcoData1); } } //add each row to the table DocItemTable.Rows.Add(trow); } this.btnExport.Visible = true; this.btnPrint.Visible = true; this.lblQryRslt.Visible = true; } I have wired the button click event of this buttone(button named-> btnViewDetails in above code) in InitializeComponent()as follwos: this.btn.Click +=new System.EventHandler(this.btnViewDetails_Click); but somehow this button event does not fire up when the table is displyed and button in the last coulmn is clicked. Any suggestion as to why this event does not fire up? I even tried to put above event in "override protected void OnInit(EventArgs e)", but it does not show any result. Thanks for any help in advance.
-
I have an asp.net application. In response to a button(on the form) click event I create a asp.net table and create rows and column. These rows and coulmn's data I extract from dataset and fill the contnet of columns and rows of the table. As I want to have each row vertically displayed, I use asp.table control instead of DataGrid control. At the end of each row I have embeded a button control in last column. Code is as follwos: private void btnRunTcl_Click(object sender, System.EventArgs e) { DataSet ds = Test(); int counter = 0; //loop over columns in a row foreach(DataColumn dc in ds.Tables[0].Columns) { TableRow trow = new TableRow(); TableCell tcellcolname; tcellcolname = new TableCell(); tcellcolname.Text = dc.ColumnName; trow.BackColor = System.Drawing.Color.Beige; tcellcolname.BackColor = System.Drawing.Color.AliceBlue; //add column name to row tcellcolname.Controls.Add(new LiteralControl(dc.ColumnName.ToString())); trow.Cells.Add(tcellcolname); TableCell tcellcoData; //count the nr of columns to be added as rows int cnt = ds.Tables[0].Columns.Count; //loop over rows in table foreach(DataRow dr in ds.Tables[0].Rows) { tcellcoData = new TableCell(); //add column content to row tcellcoData.Controls.Add(new LiteralControl(dr[dc.ColumnName].ToString())); trow.Cells.Add(tcellcoData); counter++; //after last row(column) add button if(cnt == counter) { btn.ID="btnViewDetails"; btn.Font.Bold=true; btn.Text="ViewDetails"; TableCell tcellcoData1 = new TableCell(); tcellcoData.Controls.Add(btn); trow.Cells.Add(tcellcoData1); } } //add each row to the table DocItemTable.Rows.Add(trow); } this.btnExport.Visible = true; this.btnPrint.Visible = true; this.lblQryRslt.Visible = true; } I have wired the button click event of this buttone(button named-> btnViewDetails in above code) in InitializeComponent()as follwos: this.btn.Click +=new System.EventHandler(this.btnViewDetails_Click); but somehow this button event does not fire up when the table is displyed and button in the last coulmn is clicked. Any suggestion as to why this event does not fire up? I even tried to put above event in "override protected void OnInit(EventArgs e)", but it does not show any result. Thanks for any help in advance.
Hi, I have a couple of ideas...I've only done this kind of thing a few times, so please don't take any of the following as gospel. When I need to do something like this, creating a custom web control class seems to really simplify matters. Basically you: 1) write the control as a class the derives from WebControl. 2) Override the CreateChildControls() method to create your stuff. 3) Override Render() to get it to draw correctly (mostly just for designer support). Below is a demo control that draws a table of 10 buttons. Remember, you need to keep a reverence to each Button object around if you want the event handlers to work. I hope this helps...please let me know if you're still stuck. Bill
using System; using System.Web.UI; using System.Web.UI.WebControls; namespace WatsonControls { /// /// Summary description for ctrlDemo. /// public class ctrlDemo: WebControl,INamingContainer { Button[] btns; int iRows=10; public ctrlDemo() { } protected override void CreateChildControls() { this.btns = new Button [iRows]; Controls.Add (new LiteralControl ("")); Controls.Add (new LiteralControl ("")); Controls.Add (new LiteralControl ("")); Controls.Add (new LiteralControl ("")); for (int i=0;i")); Controls.Add (new LiteralControl ("")); Controls.Add (new LiteralControl ("")); } Controls.Add (new LiteralControl (" A columns of buttons")); Controls.Add (new LiteralControl (" ")); this.btns[i]=new Button(); this.btns[i].Text = "Button: " + i.ToString (); this.btns[i].Click +=new EventHandler(ctrlDemo_Click); Controls.Add (this.btns[i]); Controls.Add (new LiteralControl (" ")); } protected override void Render(System.Web.UI.HtmlTextWriter writer) { this.EnsureChildControls (); base.Render (writer); } private void ctrlDemo_Click(object sender, EventArgs e) { //do something Page.Response.Write("Clicked"); } } }