如何从单元格内的控件获取表格行

时间:2014-01-18 05:01:40

标签: c# asp.net row parent-child

我在Page.aspx

中有这个表
<asp:Table ID="table1" runat="server" CssClass="tabla" ></asp:Table>

我使用foreach从列表中动态构建我的Page.aspx.cs table1,添加了3个单元格:

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);

我希望我的ImageButton有一个onClick事件,并从那里获取我点击的ImageButton的父行的表行id(表中的索引)。那可能吗?有什么想法吗?

5 个答案:

答案 0 :(得分:4)

试试这个:

protected void Page_Load(object sender, EventArgs e)
{
        for (int i = 0; i < 3; i++)
        {
            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();
            button.CommandArgument=i.ToString();
            button.Click += RowClick;
            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);
        }
}
protected void RowClick(object sender, EventArgs e)
{
        int rowIndex =int.Parse( ((ImageButton)sender).CommandArgument);
        Response.Write("RowIndex = " + rowIndex);
}

答案 1 :(得分:3)

在点击事件处理程序中:

ImageButton btn = sender as ImageButton;
TableCell tc = btn.Parent as TableCell;
TableRow tr = tc.Parent as TableRow;

答案 2 :(得分:1)

这是添加点击事件处理程序

的方法
  button .Click += new ImageClickEventHandler(button _Click);

...

 void button _Click(object sender, ImageClickEventArgs e)
{
     ......

答案 3 :(得分:1)

除了在单击的控件中使用CommandArgument属性之外的其他可能解决方案:

protected void btn_Click(object sender, EventArgs e)
{
    ImageButton button = sender as ImageButton;
    TableCell cell = button.Parent as TableCell;
    TableRow row = cell.Parent as TableRow;
    int index = table1.Rows.GetRowIndex(row);
}

index变量获取table1中的行索引。该解决方案基于@Aheho给出的答案。

答案 4 :(得分:0)

即使已经有一个已接受的答案,一个更通用的解决方案,不需要任何控制标记 - 给定任何可能是TableRow的控件或其中包含的任何内容,请从OnClick事件或任何地方调用此方法。

int GetRowIndexFromControl(Control control)
{
  Table table = null;
  TableRow row = null;
  while(control != null && (table == null || row == null))
  {
    if (row == null) row = control as TableRow;
    if (table == null) table = control as Table;
    control = control.Parent;
  }
  return row == null || table == null ? -1 : table.Rows.GetRowIndex(row);
}

protected void btn_Click(object sender, EventArgs e)
{
  int rowIndex = GetRowIndexFromControl(sender as Control);
  if (rowIndex != -1)
  {
    // Do something with rowIndex
  }
}