0

基本上我正在尝试填充一个部门表,并根据更改日期,为某个日期范围内的任何部门创建一个按钮。问题是,按钮创建但没有数据,

这是我到目前为止所拥有的 - onPageLoad 我绑定了所有工作并显示的数据源,直到我尝试 iTemDataBound

ASP.net

<asp:Repeater ID="DepartmentsList" runat="server" OnItemDataBound="DepartmentsList_ItemDataBound"/>
<HeaderTemplate>
     <table id="grouptable" class="table table-bordered table-striped"> 
      <thead>
              <tr>
          <th>Send</th>
          <th>ID</th>
          <th>Name</th>
          <th>Last Modified</th>
          <th>Actions</th>
          </tr>
           </thead>
           <tbody>
</HeaderTemplate>
<ItemTemplate>
              <tr>
                  <td>
                  <input type="checkbox" name="SendSelect[]" value="<%# Eval("Dept_ID") %>"</input></td>
                  <td><%# Eval("Dept_ID") %></td>
                  <td><a href="<%# Eval("gURL") %>"><%# Eval("DESC") %></a> </td>
                  <td><asp:Label ID="chg_date" runat="server" Text='<%# Eval("chg_date") %>'></asp:Label></td>
                  <td><a class="btn btn-info" href="<%# Eval("gURL") %>"><i class="icon-pencil icon-white"></i> Edit </a>&nbsp;&nbsp<asp:Button ID="bcastBtn" runat="server" Text="Send Now" CssClass="btn btn-danger" /> </td>
              </tr>
</ItemTemplate>
<FooterTemplate>
      </tbody>
      </table>
</FooterTemplate>
</asp:Repeater>

c# 代码

protected void DepartmentsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) 
            {
                return;
            }
            else
            {
                Repeater bcastbuttonControl = (Repeater)item.FindControl("bcastBtn");
                Repeater DepartmentsList = (Repeater)item.FindControl("chg_date");

                //Coding for date validation to go here - at the minute just checking based on empty or null
                if (String.IsNullOrEmpty(chg_date.Text))
                {
                    bcastBtn.Visible = false;

                }
                else
                {
                    bcastBtn.Visible = true;
                }

            }
        }
4

1 回答 1

2
Repeater bcastbuttonControl = (Repeater)item.FindControl("bcastBtn");
Repeater DepartmentsList = (Repeater)item.FindControl("chg_date");

我认为上面的代码有问题。您应该将第一个转换为按钮,将第二个转换为文本框(或任何控件,但它肯定不是中继器),当您添加 onitemdatabound 时,您的数据可能会因为无效转换异常而消失

RepeaterItem item = e.Item;

删除上面的代码,而是简单地使用:

Label chg_date = (Label)e.Item.FindControl("chg_date");

如果这不起作用,您可以发布您的 page_Load 代码吗?

于 2012-08-14T11:55:03.200 回答