Search code examples
c#asp.netdatatablerepeater

Use variable from loop in code behind to populate table


This is what I have on my aspx page

<thead>
    <tr>
        <th>Line#</th>
        <th>Item</th>
        <th>Quantity</th>
        <th>Status</th>
   <%-- Make this column visible only for a certain condition--%>
        <th>Inventory</th>
   <%----%>
    </tr>
</thead>
    <tbody>
        <asp:repeater id="shoppingcartlines" runat="server">
                <itemtemplate>
                      <tr>
            <td><%# DataBinder.Eval(Container.DataItem,"LineNo") %></td>
            <td><%# DataBinder.Eval(Container.DataItem,"ItemName")%></td>
            <td><%# DataBinder.Eval(Container.DataItem,"Quantity")%></td>
            <td><%= StatusVal  %></td>
            <td><%= InvVal  %></td>
                     </tr>
               </itemtemplate>
        </asp:repeater> 
    </tbody>

This is what I have in my aspx.cs page ( code-behind )

foreach ()
{
 StatusVal = loopdetail.Status; // string
 InvVal = loopdetail.InvVal; // string

}

Problem 1 : Desired output for a particular loop :

Status Inv

False   10
True    20

Output now :

Status  Inv

 True    20
 True    20

The StatusVal variable is displaying the last grabbed value from the loop. I want them to display it line by line as shown above.

I am doing this on the page load event.

protected void Page_Load(object sender, EventArgs e)
  {
   foreach (ItemDetail loopdetail in Custom.ItemDetails)
            {
                // StatusVal and InvVal are public strings
                StatusVal = loopdetail.Status; // string
                InvVal = loopdetail.InvVal; // string

            }
  }

Problem 2 :

I want to display the Inventory Column only if it matches a certain condition. Should I be changing my aspx file or aspx.cs ?


Solution

  • Problem 1: This is how I know how to do it:

    Datatable dt = new Datatable();
    dt.Columns.Add("StatusVal", typeof(bool));
    dt.Columns.Add("InvVal", typeof(string));
    
    foreach()
    {
        dt.Rows.Add(new object[2]{loopdetail.Status, loopdetail.InvVal});
    }
    
    shoppingcartlines.DataSource = dt;
    shoppingcartlines.DataBind();
    

    Maybe this solution is obvious to you and there are limitations to you being able to do this, just thought I would mention it.

    Problem 2:

    Given whatever condition you want to use in order to determine visibility, one thing you could do is:

    protected void shoppingcartlines_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RepeaterItem item = e.Item;
        HtmlTableCell td = (HtmlTableCell)item.FindControl("idOfTdYouWantToHide");
    
        if (iWantToDisplay)
        {
            td.Visible = true;
            // if that doesn't work, just do:
            //    td.Style.Add("display", "none");
        }
    }
    

    I should also state that the property, "OnItemDataBound", should be added to your repeater, and reference the method above:

    OnItemDataBound="shoppingcartlines_ItemDataBound"
    

    Lastly, don't forget to add add

    <%= Bind("StatusVal") %>
    

    and

    <%= Bind("InvVal") %>
    

    to your td elements.