ItemDataBound没有触发

时间:2014-01-10 11:12:45

标签: c# asp.net

出于某种原因,我的中继器上的itemdatabound只是没有开火。我无法理解为什么。继承人我所拥有的; (删除了不必要的东西)

C#页码:

 protected void Page_Load(object sender, EventArgs e)
        {

            load_Repeater();
        }

        private void load_Repeater()
        {
            dtLocations = new components.db.PIDSDB.SubPIDLocationsItemsDataTable();
            dtLocations = taLocations.GetLocationsByProductsAndSPID('1', Convert.ToInt32(Request.QueryString["SPID"]));

            rptFire.DataSource = dtLocations;
            rptFire.DataBind();

        }

        protected void rptFire_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            DataRowView nRow = null;

            switch (e.Item.ItemType)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                Int32 sqrmtr = Convert.ToInt32(nRow["LocationSqrMtr"].ToString());
                Int32 mypence = Convert.ToInt32(nRow["PricePerItem"].ToString());
                Int32 mandays = Convert.ToInt32(nRow["ManDayPerItem"].ToString());

                ((Label)e.Item.FindControl("lblName")).Text = "" + nRow["LocationName"];
                ((Label)e.Item.FindControl("lblArea")).Text = "" + nRow["LocationSqrMtr"];
                ((Label)e.Item.FindControl("lblCharge")).Text = ""+ Functions.MultiplyNumbers(sqrmtr, mypence);
                ((Label)e.Item.FindControl("lblPence")).Text = "" + nRow["PricePerItem"];
                ((Label)e.Item.FindControl("lblManDays")).Text = "" + Functions.MultiplyNumbers(sqrmtr, mandays);
                ((Label)e.Item.FindControl("lblCosts")).Text = "WORK OUT";
                ((Label)e.Item.FindControl("lblMargin")).Text = "WORK OUT";

                    break;
            }
        }

在.aspx页面转发器代码:

   <asp:Repeater runat="server" ID="rptFire" OnItemDataBound="rptFire_ItemDataBound">
       <ItemTemplate>
          <tr>
              <td><asp:Label runat="server" ID="lblName"></asp:Label></td>
              <td><asp:Label runat="server" ID="lblArea"></asp:Label></td>
              <td><asp:Label runat="server" ID="lblCharge"></asp:Label></td>
              <td><asp:Label runat="server" ID="lblPence"></asp:Label></td>
               <td><asp:Label runat="server" ID="lblManDays"></asp:Label></td>
               <td><asp:Label runat="server" ID="lblCosts"></asp:Label></td>
               <td><asp:Label runat="server" ID="lblMargin"></asp:Label></td>
            </tr>
          </ItemTemplate>
        </asp:Repeater>

数据源和数据绑定代码不运行itemdatabound

1 个答案:

答案 0 :(得分:1)

您可能在aspx文件的页眉中缺少AutoEventWireup=true

修改 如果按钮处理程序可以正常工作,可能是数据处理问题 - 您确定在DataBind或其任何父控件上调用MyRepeater吗?如果您手动拨打DataBind,是否依赖于您网页上的Page.IsPostBack

ItemDataBound仅在DataBinding期间触发。如果你在PostBack上做一个DataBind,它应该触发。但是,DataBind不会在PostBack上自动发生。 (相反,Repeater将从ViewState重建。)如果没有对数据的依赖,则应考虑使用ItemCreated事件。无论是在DataBind期间还是从ViewState开始,构建Repeater时都会触发。