不要在后面的代码中创建整个表格标记,而是使用 ASP.NET 提供的控件。
例如,将其放在您的 .aspx 中:
<table>
<asp:Repeater runat="server" ID="MyRepeater1">
<ItemTemplate>
<tr>
<td><%# Eval("StudentName")%></td>
<td>... other student object bindings ...</td>
<td>
<asp:Button runat="server" ID="MyDeleteButton"
CommandArgument='<%# Eval("ID")%>'
CommandName="Delete"
OnCommand="MyDeleteButton_Command"
Text="X" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
并将其包含在您的代码隐藏中:
protected void Page_Load(object sender, EventArgs e)
{
MyRepeater1.DataSource = new MyStudent[]
{
new MyStudent()
{
ID = 1,
StudentName = "Student 1"
},
new MyStudent()
{
ID = 2,
StudentName = "Student 2"
}
};
MyRepeater1.DataBind();
}
protected void MyDeleteButton_Command(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "Delete":
// stuff for deleting by e.CommandArgument
break;
}
}