How to get a table row from a control inside a cell
NickName:anairinac Ask DateTime:2014-01-18T13:01:39

How to get a table row from a control inside a cell

I have this table in my Page.aspx

<asp:Table ID="table1" runat="server" CssClass="tabla" ></asp:Table>

I am building dynamically table1 in my Page.aspx.cs from a list using a foreach, adding 3 cells:

TableCell cell_name = new TableCell();
cell_name.Text = "Some name";
TableCell cell_active = new TableCell();
CheckBox checkbox = new CheckBox();
cell_active.Controls.Add(checkbox);
TableCell cell_actions = new TableCell();
ImageButton button = new ImageButton();
cell_actions.Controls.Add(button);

TableRow row = new TableRow();
row.Cells.Add(cell_name);
row.Cells.Add(cell_active);
row.Cells.Add(cell_actions);

table1.Rows.Add(row);

I want my ImageButton to have a onClick event and get from there the table row id (index inside the table) for the parent row of my ImageButton that was clicked. Is that possible? Any ideas?

Copyright Notice:Content Author:「anairinac」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/21200212/how-to-get-a-table-row-from-a-control-inside-a-cell

Answers
Samiey Mehdi 2014-01-18T06:27:56

Try this:\n\nprotected void Page_Load(object sender, EventArgs e)\n{\n for (int i = 0; i < 3; i++)\n {\n TableCell cell_name = new TableCell();\n cell_name.Text = \"Some name\";\n\n TableCell cell_active = new TableCell();\n CheckBox checkbox = new CheckBox();\n cell_active.Controls.Add(checkbox);\n\n TableCell cell_actions = new TableCell();\n ImageButton button = new ImageButton();\n button.CommandArgument=i.ToString();\n button.Click += RowClick;\n cell_actions.Controls.Add(button);\n\n TableRow row = new TableRow();\n row.Cells.Add(cell_name);\n row.Cells.Add(cell_active);\n row.Cells.Add(cell_actions);\n\n table1.Rows.Add(row);\n }\n}\nprotected void RowClick(object sender, EventArgs e)\n{\n int rowIndex =int.Parse( ((ImageButton)sender).CommandArgument);\n Response.Write(\"RowIndex = \" + rowIndex);\n}\n",


Aheho 2014-01-18T05:09:17

In the click event handler:\n\nImageButton btn = sender as ImageButton;\nTableCell tc = btn.Parent as TableCell;\nTableRow tr = tc.Parent as TableRow;\n",


zey 2014-01-18T05:10:25

This is how to add click event handler \n\n button .Click += new ImageClickEventHandler(button _Click);\n\n\n..\n\n void button _Click(object sender, ImageClickEventArgs e)\n{\n ......\n",


anairinac 2014-01-18T08:27:09

Other possible solution besides using CommandArgument attribute in the clicked Control:\n\nprotected void btn_Click(object sender, EventArgs e)\n{\n ImageButton button = sender as ImageButton;\n TableCell cell = button.Parent as TableCell;\n TableRow row = cell.Parent as TableRow;\n int index = table1.Rows.GetRowIndex(row);\n}\n\n\nindex variable gets the row index in table1. This solution is based on the answer given by @Aheho.",


More about “How to get a table row from a control inside a cell” related questions

How to get a table row from a control inside a cell

I have this table in my Page.aspx &lt;asp:Table ID="table1" runat="server" CssClass="tabla" &gt;&lt;/asp:Table&gt; I am building dynamically table1 in my Page.aspx.cs from a list using a fore

Show Detail

table get the cell from the row

I have one table with 3 rows and 3 columns. Now I want to add 2 row's 2nd columns cell's control. That means whether that is text or combox in the cell. How do I get the 2nd row's 2nd column and r...

Show Detail

How to get selected table view cell row in segue?

I am developing an iOS app using Swift and I have a view controller that segues from a table view cell content view by selecting a cell row or selecting a button inside of that cell row's content v...

Show Detail

How to get checkbox inside table row cell

I'm very noob in javascript. I'm trying to alert the id of a checkbox inside table view row cell. I tried: var checkBox = grid.rows[i].cells[0].firstChild; alert(checkBox.id); but the alert box...

Show Detail

How to dynamically get cell value from SQL table?

I am new to SQL. As show in screenshot, how can I get a any specific cell value according to column name? This query can get you specific cell value according to the column name inside the quer...

Show Detail

How to check what control is inside a table cell

I'm trying to check what control I have inside a table cell and if that control is a checkbox, check it. What I have so far looks like this: var x = document.getElementById('

Show Detail

How to get the position(col, row) of the user changed cell in a Table control?

I'm using tutorial_Form_Table. The table has dynamically assigned values, something like: for (col = 1; col &lt;= 5; col++) { for (row = 1; row &lt;= 5; row++) { if ((col =

Show Detail

How To Hide/Unhide Specific Table Cell/Row

A very newbie in XCOde.. Currently Im using 4.2. Im making a walk through of an app that do specific function. Im just doing step by step process how the app should work/flow. I just want to know...

Show Detail

How to get the clicked cell inside the click event handler of a table row?

There is a html table populated dynamically from an ajax , on which there is a click handler on the tr element : $("#table").on("click" ,"tr", function() { // working codes } I want to get the

Show Detail

How get control from cell in my DataGridView?

I would like to know if it's possible to directly get control of a specific cell by using its references / column and row indexes. I noticed that it was possible to get control of a cell from an e...

Show Detail