gpt4 book ai didi

c# - 将 foreach 循环中的值分配给 asp :button CommandArgument

转载 作者:行者123 更新时间:2023-11-30 13:52:40 24 4
gpt4 key购买 nike

代码隐藏文件创建一个员工列表,而 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>
<%}%>

最佳答案

根据您的评论,这是一个嵌套示例:

标记:

<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();
}
}

}

关于c# - 将 foreach 循环中的值分配给 asp :button CommandArgument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1658967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com