而不是在您的代碼中創建整個表標記,使用由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;
}
}
是表一個GridView或中繼器?或數據專家? –
你是什麼意思的「不工作」? –
您所做的只是使用您創建的按鈕創建一個帶有ClientID字符串的單元格。你實際上並沒有以這種方式向頁面添加按鈕。 –