ASP web形成ASCX用户控制转发器中的循环

时间:2017-09-12 09:56:45

标签: asp.net for-loop syntax webforms user-controls

我有一个转发器,其模型具有每个月的状态。

所以我想在转发器中使用for循环,但问题是变量无法访问

代码:

<ItemTemplate>
<% for (int month = 1; month <= 12; month++)
  { %>
   <div class="DS_monthCol <%= GetStatusClassName(Item, month) %>">
      <asp:HyperLink ID="HyperLink1" runat="server"></asp:HyperLink>
  </div>

<% } %>
</ItemTemplate>

方法GetStatusClassName可以访问 - 没问题。但是这种情况:The name Item does not exits in the current context,这是Visual Studio的错误。

如果我使用它:

<%# GetStatusClassName(Item, month) %>

The name month does not exits in the current context

如何让两者都可以访问?

我们的想法是避免这种情况:

<div class='DS_monthCol <%# GetStatusClassName(Item, 1) %>'>
    <asp:HyperLink ID="HyperLink1" runat="server"></asp:HyperLink>
</div>
<div class='DS_monthCol <%# GetStatusClassName(Item, 2) %>'>
    <asp:HyperLink ID="HyperLink2" runat="server"></asp:HyperLink>
</div>
<div class='DS_monthCol <%# GetStatusClassName(Item, 3) %>'>
    <asp:HyperLink ID="HyperLink3" runat="server"></asp:HyperLink>
</div>
<div class='DS_monthCol <%# GetStatusClassName(Item, 4) %>'>
    <asp:HyperLink ID="HyperLink4" runat="server"></asp:HyperLink>
</div>
<div class='DS_monthCol <%# GetStatusClassName(Item, 5) %>'>
    <asp:HyperLink ID="HyperLink5" runat="server"></asp:HyperLink>
</div>
<div class='DS_monthCol <%# GetStatusClassName(Item, 6) %>'>
    <asp:HyperLink ID="HyperLink6" runat="server"></asp:HyperLink>
</div>
<div class='DS_monthCol <%# GetStatusClassName(Item, 7) %>'>
    <asp:HyperLink ID="HyperLink7" runat="server"></asp:HyperLink>
</div>
<div class='DS_monthCol <%# GetStatusClassName(Item, 8) %>'>
    <asp:HyperLink ID="HyperLink8" runat="server"></asp:HyperLink>
</div>
<div class='DS_monthCol <%# GetStatusClassName(Item, 9) %>'>
    <asp:HyperLink ID="HyperLink9" runat="server"></asp:HyperLink>
</div>
<div class='DS_monthCol <%# GetStatusClassName(Item, 10) %>'>
    <asp:HyperLink ID="HyperLink10" runat="server"></asp:HyperLink>
</div>
<div class='DS_monthCol <%# GetStatusClassName(Item, 11) %>'>
    <asp:HyperLink ID="HyperLink11" runat="server"></asp:HyperLink>
</div>
<div class='DS_monthCol <%# GetStatusClassName(Item, 12) %>'>
    <asp:HyperLink ID="HyperLink12" runat="server"></asp:HyperLink>
</div>

1 个答案:

答案 0 :(得分:0)

您可以尝试使用ItemDataBound事件

<ItemTemplate>
<asp:PlaceHolder ID="ph" runat="server" />
</ItemTemplate>



string GetStatusClassName(Object obj, int month)
        {
            //your existing function
        }

将ItemDataBound事件用作:

protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {

                //find place holder and add links in this place holder
                PlaceHolder ph = (PlaceHolder)e.Item.FindControl("ph");

                //get your object of datasource assigned to repeater 
                Object dataItem = e.Item.DataItem;//need to add you items source

                for (int month = 1; month <= 12; month++)
                {
                    //create div as per your example else you can directly add links to panel and handled display with css             
                    HtmlGenericControl div = new HtmlGenericControl("div");

                    // set id, class to whatever you want
                    div.Attributes.Add("class", "DS_monthCol " + GetStatusClassName(dataItem, month));

                    // declare a textbox
                    HyperLink lnk = new HyperLink();

                    //add link in PlaceHolder           
                    div.Controls.Add(lnk);

                    ph.Controls.Add(div);
                }


            }
        }
  

注意:此代码未经过数据测试。