3

The code behind file creates a list of employees and the asp.net page loops through list. Now, How do I assign the employeedID to the commandArgument of an asp:button?

  <%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>
<%}%>
2
  • Is this MVC code or WebForms? Commented Nov 2, 2009 at 1:07
  • It's webforms.Please see my comment for Aaron's answer. Commented Nov 2, 2009 at 1:12

2 Answers 2

7

Based on your comments, here is a nested example:

Markup:

<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>

CodeBehind:

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

}
Sign up to request clarification or add additional context in comments.

2 Comments

Glad I saw this before posting almost exactly the same thing!
Thank you Rick. That worked.copy/paste issue in the code.Should be: if(r!=null){ r.DataSource = innerDataSource; r.DataBind(); }
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>

and then in the codebehind

if (!Page.IsPostBack)
{
    rptEmployeeList.DataSource = employeeList;
    rptEmployeeList.DataBind();
}

2 Comments

Is it possible using the foreach loop, because in this case , I need a nested for loop to go through the projects that the employee is working on (an example). Or alternately, Can I have a nested asp:Repeater ?
_rick_schott has a nested example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.