題主將多行數據拼接字符串賦給單個 `Label.Text`,再想在 `Label.Click` 中知道用戶點擊了第幾行 — **Label 不具備行級點擊感知能力**,渲染出來只是一塊純文本。
| 場景 | 推薦控件 | 核心屬性/事件 |
|---|---|---|
| WebForms 列表 | `Repeater` / `ListView` / `GridView` | `ItemCommand` / `RowCommand` + `CommandArgument` 綁定索引/ID |
| WinForms 列表 | `ListView` (Details) / `DataGridView` | `ItemActivate` / `CellClick` + `Tag` / `Rows[e.RowIndex].Cells["Id"].Value` |
| WPF 列表 | `ListView` / `DataGrid` / `ItemsControl` | `MouseDoubleClick` + `SelectedItem` (MVVM 綁定命令) |
<asp:Repeater ID="rptData" runat="server" OnItemCommand="rptData_ItemCommand">
<ItemTemplate>
<a href="#" class="item-link"
<%# "onclick=\"return doClick(" + Container.ItemIndex + ");\"" %>>
<%# Eval("G_Title") %>
</a><br />
</ItemTemplate>
</asp:Repeater>
protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e) {
int index = e.Item.ItemIndex; // 獲取行索引
string title = (e.Item.FindControl("lblTitle") as Label).Text;
// 業務邏輯
}
結論:**不要嘗試用 Label 承載列表交互**,選對控件事半功倍。