Search code examples
c#asp.netrepeater

Repeater ItemDataBound event gives null exception


I am getting null exception when using itemdatabound.when I chech ,what I see is item index zero.can u tell me where my repeater is wrong

my repeater ;

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

and code behind ItemDatabound event

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;


        }

Solution

  • Imad is correct you need to check that you are searching in Item rows.

    You could find your controls in the following manner to determine the cause of the error:

            Label lblKonutFiyat = e.Item.FindControl("lblKonutFiyat") as Label;
    
            if (lblKonutFiyat != null)
            {
                lblKonutFiyat.Text = MyResource.lblKonutFiyat;
            }
    
           ....
    

    If your control is null it will gracefully handle it, any labels that aren't getting set will provide you with the problematic labels.