2015-04-27 58 views
0

這是我對我的ASPX頁面使用變量後面填充表

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

這是我在我的aspx.cs頁面(代碼隱藏)

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

} 

問題1: 希望的輸出對特定循環:

Status Inv 

False 10 
True 20 
現在

輸出:

Status Inv 

True 20 
True 20 

的StatusVal變量顯示從循環中的最後抓起值。我想讓他們按照上圖所示逐行顯示它。

我正在做這個頁面加載事件。

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 

      } 
    } 

問題2:

我想僅當它相匹配的特定條件,以顯示清單列。我應該改變我的aspx文件還是aspx.cs?

+2

什麼是StatusVal ..它的類型是什麼..?你也許想創建一個'List '你可以發佈所有相關的代碼嗎? – MethodMan

+0

你需要將這些額外的值添加到綁定到中繼器的對象,並使用數據綁定事件來顯示/隱藏所需的列。 –

+0

@MethodMan StatusVal是一個字符串。循環似乎工作得很好。我想事實上我的aspx代碼有​​<%= StatusVal%>,它只取最後一個初始值。 – thestralFeather7

回答

1

問題1: 這是我知道如何做到這一點:

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

也許這個解決方案是顯而易見的你,還有你能做到這一點的限制,只是想我會提到它。

問題2:

鑑於你想,以確定可視性使用任何條件,有一兩件事你可以做的是:

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

我還要指出的財產,「OnItemDataBound 「,應該添加到你的中繼器,並參考上面的方法:

OnItemDataBound="shoppingcartlines_ItemDataBound" 

最後,別忘了給一個DD添加

<%= Bind("StatusVal") %> 

<%= Bind("InvVal") %> 

你的td元素。

+0

所以我只爲2列製作不同的表格?然後將其綁定到新的數據源? – thestralFeather7

+0

您應該可以將它保留在同一個表格中。只需將<%= Bind(「StatusVal」)%>和<%= Bind(「InvVal」)%>添加到td元素即可。 –

+1

你能弄明白嗎? –