Repeater ItemDataBound事件给出null异常

时间:2014-01-30 11:18:34

标签: c# asp.net repeater

使用itemdatabound时我得到null异常。当我知道时,我看到的是项目索引zero.can你告诉我我的转发器错在哪里

我的转发器;

        <asp:Repeater runat="server" ID="rptKonutm" OnItemDataBound="rptKonut_ItemDataBound">
            <ItemTemplate>


      <div class="caption">
      <h4 class="pull-right"><%# DataBinder.Eval(Container.DataItem, "Fiyat") %>$</h4>
       <h4>
       <asp:Label runat="server" ID="lblKonutFiyat"></asp:Label>

        </h4>
        <h4 class="pull-right"><%# DataBinder.Eval(Container.DataItem, "Il")%></h4>
        <h4><asp:Label runat="server" ID="lblMevki"></asp:Label></h4>

          <h4 class="pull-right"><%# DataBinder.Eval(Container.DataItem, "EkBilgi")%></h4>
        <h4><asp:Label runat="server" ID="lblDahaFazlaBilgi"></asp:Label></h4>
        <h4 class="pull-right"><%# DataBinder.Eval(Container.DataItem, "AlanMetreKare")%></h4>
          <h4><asp:Label runat="server" ID="lblAlanMetreKare"></asp:Label></h4>
 </div>
                    <!-- Split button -->
                   </ItemTemplate>
        </asp:Repeater>

和ItemDatabound事件背后的代码

protected void rptKonut_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            System.Web.UI.WebControls.Label lblKonutFiyat = (System.Web.UI.WebControls.Label)e.Item.FindControl("lblKonutFiyat");
            System.Web.UI.WebControls.Label lblMevki = (System.Web.UI.WebControls.Label)e.Item.FindControl("lblMevki");
            System.Web.UI.WebControls.Label lblDahaFazlaBilgi = (System.Web.UI.WebControls.Label)e.Item.FindControl("lblDahaFazlaBilgi");
            System.Web.UI.WebControls.Label lblAlanMetreKare = (System.Web.UI.WebControls.Label)e.Item.FindControl("lblAlanMetreKare");


            lblKonutFiyat.Text = MyResource.lblKonutFiyat;
            lblMevki.Text = MyResource.lblMevki;
            lblDahaFazlaBilgi.Text = MyResource.lblDahaFazlaBilgi;
            lblAlanMetreKare.Text = MyResource.lblAlanMetreKare;


        }

2 个答案:

答案 0 :(得分:2)

如果

,请尝试将代码保留在以下代码中
 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem){}

答案 1 :(得分:2)

Imad是正确的,你需要检查你在Item行中搜索。

您可以通过以下方式找到控件,以确定错误原因:

        Label lblKonutFiyat = e.Item.FindControl("lblKonutFiyat") as Label;

        if (lblKonutFiyat != null)
        {
            lblKonutFiyat.Text = MyResource.lblKonutFiyat;
        }

       ....

如果您的控件为空,它会正常处理它,任何未设置的标签都会为您提供有问题的标签。