2014-01-18 37 views
1

我有這個表裏面我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事件,並從那裏錶行ID獲得(表內指數)爲被點擊我的ImageButton的父行。那可能嗎?有任何想法嗎?

+0

通過你的代碼,你只有一行,不是嗎? – zey

+0

我有幾行添加到表中。其實我的代碼是在'foreach(var list in list)'裏面。 – anairinac

回答

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); 
} 
除了在被點擊的控制使用 CommandArgument屬性10
+0

@anairinac,幫忙? –

+0

是的,它做到了!這就是它。謝謝! – anairinac

3

在單擊事件處理程序:

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

這是工作,除了我不得不將tc和tr投射到他們的類型。現在的問題是我得到了我的行,但需要我的行索引在表內。我想它就像一個列表,範圍從0到rowcount-1,這是我需要在我的onClick事件上得到的int。 – anairinac

1

這是如何添加單擊事件處理程序

button .Click += new ImageClickEventHandler(button _Click); 

..

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

其它可能的解決方案:

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給出的答案。

0

儘管已經有一個可接受的答案,但不需要任何控制標記的更通用的解決方案 - 給定任何可能是TableRow或其中包含的任何控件的Control,都可以從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 
    } 
} 
相關問題