代码隐藏文件创建了一个员工列表,asp.net页面循环遍历列表。 现在,如何将employeedID分配给asp:button的commandArgument?
<%foreach(var employee in employeesList){%>
<tr>
<td><%=employee.firstName%></td>
<td><%=employee.lastName%></td>
/*How to access employee.Id in the commandArgument? */
<td><asp:Button ID="btnDeleteEmployee" runat="server" CommandArgument='' Text="Delete" OnCommand="btnDeleteEmployee_Click" /></td>
</tr>
<%}%>
答案 0 :(得分:6)
根据您的评论,这是一个嵌套示例:
<强>标记:强>
<asp:Repeater runat="server" ID="rptOutter"
onitemdatabound="rptOutter_ItemDataBound" >
<ItemTemplate>
<tr>
<td><%#Eval("firstName") %></td>
<td><%#Eval("lastName") %></td>
<td><asp:Button runat="server" ID="btnDeleteEmployee" CommandArgument='<%#Eval("Id") %>' Text="Delete" OnCommand="btnDeleteEmployee_Click" /></td>
<td>
<asp:Repeater runat="server" ID="rptInner" >
<ItemTemplate>
<table>
<tr>
<td><%#Eval("firstNameInner") %></td>
<td><%#Eval("lastNameInner") %></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
<强>代码隐藏:强>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
rptOutter.DataSource = outterDataSource;
rptOutter.DataBind();
}
}
protected void rptOutter_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
if ((item.ItemType == ListItemType.Item) ||
(item.ItemType == ListItemType.AlternatingItem))
{
//get your datasource from parent repeater if needed
//cast to your datasource type
//DataSourceObject ds = (DataSourceObject)item.DataItem;
Repeater r = e.Item.FindControl("rptInner") as Repeater;
if (r != null)
{
r.DataSource = innerDataSource;
r.DataBind();
}
}
}
答案 1 :(得分:3)
<asp:Repeater runat="server" ID="rptEmployeeList">
<ItemTemplate>
<tr>
<td><%#Eval("firstName") %></td>
<td><%#Eval("lastName") %></td>
<td><asp:Button runat="server" ID="btnDeleteEmployee" CommandArgument='<%#Eval("Id") %>' Text="Delete" OnCommand="btnDeleteEmployee_Click" /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
然后在代码隐藏
if (!Page.IsPostBack)
{
rptEmployeeList.DataSource = employeeList;
rptEmployeeList.DataBind();
}